Search Posts

Sort MySQL records according to co-ordinates

I have a MySQL Table where there are three columns: lat, lon and ID

Now as Input I want user to give a pair of Lat & long. In return, I want to show the sorted rows from closest to farthest.

Now, one solution of this problem is, fetch all entries in an array, make a temp array, calculate distance for each item using haversine formula, push to the temp array, and finally perform any sorting algorithm on that array.

But, this operation is heavy and I want a better solution. Is there any?

P.S: I am using PHP script.

Source: Stackoverflow

Leave a Reply

Your email address will not be published. Required fields are marked *