• User Newbie

    PHP-MYSQL: Utilizzo della funzione st_distance_sphere

    Ciao a tutti ho un problema e spero di riuscirvelo a spiegare.
    Innanzitutto ho un database che è composto dai seguenti campi:

    • CompanyObjectId
    • CompanyName
    • CityName
    • Latitude
    • Longitude
    • CampingLocationType
    • PhotoUrl

    Il mio obiettivo è selezionare tutti quei campi includendo anche i metri cui distano ogni riga dalla mia posizione per esempio da un minimo di 1 km ad un max di 200 a seconda di quello che gli passo in input.

    Allora il codice che ho provato a fare in PHP è questo e che sta dentro il file DBOperations.php:

    [PHP]
    public function GetListaDoveSostare($lat, $lng, $kmmin, $ordinamentoData, $localita){ $metri = (intval($kmmin) * 1000); // ricavo i metri dai km $com = new DbConnect(); $sql = ""; $sql = "SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude, CampingLocationType, PhotoUrl, st_distance_sphere(point(".floatval($lng).",".floatval($lat)."), point(CAST(Longitude AS DECIMAL(18,14)), CAST(Latitude AS DECIMAL(18,14))), $metri) as Metri FROM tbl_camper_contact WHERE st_distance_sphere(point(".floatval($lng).",".floatval($lat)."), point(CAST(Longitude AS DECIMAL(18,14)), CAST(Latitude AS DECIMAL(18,14))), $metri)"; mysqli_set_charset($com->getDb(), 'utf8'); $result = mysqli_query($com->getDb(), $sql); return $result;}[/PHP]

    Dove come input ricevo nell'ordine: latitudine utente, longitudine utente, km impostati come limite da 0 a n km su cui cercare, ordinamento (non è usato al momento), località non è usata al momento.

    Il risultato di questa funzione sarà inserito in un altra che sta dentro il file GeneralFunctions.php:

    [PHP]
    public function GetListaDoveSostare($resultDoveSostare, $lat, $lng) { //non fate caso ai due input lat e lng.... $dbOperationsObject = new DBOperations(); $doveSostare= array(); while ($row = mysqli_fetch_array($resultDoveSostare)) { $dove_sostare = array( "CompanyObjectId" => $row['CompanyObjectId'], "CompanyName" => $row['CompanyName'], "CityName" => $row['CityName'], "Latitude" => $row['Latitude'], "Longitude" => $row['Longitude'], "CampingLocationType" => ($row['CampingLocationType'] == "Camperplace") ? "Area Sosta" : "Campeggio", "PhotoUrl" => $row['PhotoUrl'], "MetriDaPosizioneCorrente" => $row['Metri']//$metri ); $json= json_encode($dove_sostare); $dove_sostare = json_decode($json); array_push($doveSostare, $dove_sostare); } return $doveSostare; } [/PHP]

    Il problema è basato sostanzialmente su questi due errori:

    [PHP]AH01215: PHP Warning: mysqli_query(): (HY000/1210): Incorrect arguments to st_distance_sphere in DBOperations.php

    AH01215: PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in in GeneralFunctions.php [/PHP]

    Ditemi dove sbaglio. Sto andando in crisi.
    Grazie
    Cristian