I have a MySQL query listing results of restaurants, hotels etc. Each row has latitude and longitude data.
I need to sort the results by distance relative to the user's searched location (I have that latitude and longitude as soon as the page is loaded).
I also have a php function which returns distance in kilometers from two lat/lng pairs:
distance($lat1,$lng1,$lat2,$lng2);
But how do I order the query results by this distance?
Let's say the query is, at the moment, simply
mysql_query("select * from merchants");
Sticking order by distance(latitude,longitude,'$center_lat','$center_long') in there gives an error saying FUNCTION dbname.distance does not exist. I've thought about creating a temporary MySQL tables with the distances as soon as the page is loaded and then sorting by that, but that seems like overkill. Is it?