距离计算不返回结果

时间:2013-06-02 01:18:43

标签: php mysql sql distance

来自api.php的代码:

    $lat = $_GET['lat'];
    $lng = $_GET['lon'];

    try {$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);}
    catch(PDOException $e){echo($e->getMessage());}

    $stmt = $dbh->prepare("SELECT *, (POW(69.1 * (`lat` - :lat), 2) +
                            POW(69.1 * (:lon - `long`) * COS(`lat` / 57.3), 2)) AS distance
                            FROM `venues` HAVING distance < SQRT(16) ORDER BY distance");
    $stmt->bindParam(':lat', $lat, PDO::PARAM_STR);
    $stmt->bindParam(':lon', $lon, PDO::PARAM_STR);


    $venues = array();
    if ($stmt->execute()) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $venues[] = $row;       
            echo $_GET['jsoncallback'] . json_encode($venues);
        }
    }

访问api.php?lat=LAT_VALUE&lon=LON_VALUE时,不会返回任何内容。

但是,当我在SQL中键入以下内容时:

SELECT * , (
POW( 69.1 * (`lat`-'LAT_VALUE'),2) + POW(69.1*('LON_VALUE'-`long`)*COS(`lat`/57.3),2)) AS distance
FROM  `venues` 
HAVING distance < SQRT( 16 ) 
ORDER BY distance
LIMIT 0 , 30

...它返回的结果很好。

我的语法出了什么问题?

2 个答案:

答案 0 :(得分:1)

$lon / $lng变量有两个名称。 试试这个:

$lon = $_GET['lon'];

答案 1 :(得分:0)

问题在这里。您在查询中使用的是:lon而不是lng

  $stmt = $dbh->prepare("SELECT *, (POW(69.1 * (`lat` - :lat), 2) +
                POW(69.1 * (:lon - `long`) * COS(`lat` / 57.3), 2)) AS distance
                  FROM `venues` HAVING distance < SQRT(16) ORDER BY distance");

改为使用

$stmt = $dbh->prepare("SELECT *, (POW(69.1 * (`lat` - :lat), 2) +
                  POW(69.1 * (:lng - `long`) * COS(`lat` / 57.3), 2)) AS distance
                   FROM `venues` HAVING distance < SQRT(16) ORDER BY distance");