2011-07-05 240 views
0

我有一個基於Haversine算法獲取地點的查詢。MySQL查詢只返回一個結果

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)); 

但是,它只返回一行時應該有至少兩個。任何人都知道爲什麼它會這樣做?謝謝!

+0

爲什麼你使用HAVING而不是WHERE和沒有GROUP BY。這個查詢是否工作? – niktrs

+0

你是否在使用mysql_fetch_assoc而不是 – Gowri

+0

@niktrs是的,除了我提到的錯誤之外,它的工作是完美無缺的。 – iamandrus

回答

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

這很好,非常感謝! – iamandrus

+0

歡迎您!plz點擊「這個答案很有用」:D –

-1

你需要在while循環中使用mysql_fetch_assoc()函數。

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

print_r($location); 

謝謝。

+0

現在它只是返回「false」。 – iamandrus

+0

mysql_query()返回什麼?你檢查mysql_error()嗎? – wonk0