MySQL查询只返回一个结果

时间:2011-07-05 06:35:51

标签: php mysql

我有一个基于Haversine算法获取位置的查询。

SELECT
 id, description, name, 
 lat, `long`, 
 ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( `long` ) - radians($long) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance 
FROM 
 places 
HAVING 
 distance < 10 
ORDER BY 
 distance 
LIMIT 0, 20;

然后我在这样的JSON数组中回显它:

$location = mysql_fetch_assoc($getlocations);
return print_r(json_encode($location));

但是,它应该只返回一行,至少应该有两行。任何人都知道为什么会这样做?谢谢!

2 个答案:

答案 0 :(得分:2)

while( $row = mysql_fetch_assoc($getlocations)){
    $location[] = $row;
}
return print_r(json_encode($location));

答案 1 :(得分:-1)

你需要在while循环中使用mysql_fetch_assoc()函数,这可能是。

i.e:
while($location = mysql_fetch_assoc($getlocations));

print_r($location);

感谢。

相关问题