0

I have a query that gets places based on the Haversine algorithm.

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;

Then I echo it out in a JSON array like this:

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

However, it only returns one row when there should be at least two. Anyone know why it might be doing this? Thanks!

7
  • Why are you using HAVING instead of WHERE and without GROUP BY. Is this query working?? Commented Jul 5, 2011 at 6:38
  • are you using mysql_fetch_assoc in while or not Commented Jul 5, 2011 at 6:38
  • @niktrs Yeah, it works flawlessly except for the error I mentioned. Commented Jul 5, 2011 at 6:39
  • I think because mysql_fetch_assoc starts at the first pointer, to get the second or so, you need to iterate. Commented Jul 5, 2011 at 6:39
  • 1
    When you have more than 1 row result you must use loop to retrieve all result! Commented Jul 5, 2011 at 6:41

2 Answers 2

2
while( $row = mysql_fetch_assoc($getlocations)){
    $location[] = $row;
}
return print_r(json_encode($location));
Sign up to request clarification or add additional context in comments.

Comments

-1

you need to use mysql_fetch_assoc() function in while loop for that may be.

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

print_r($location);

thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.