Reputation: 14588
I have a database like this
https://i.sstatic.net/MHEwr.jpg
I have a PHP function which will compute distance { get_distance ($person_location) } of that address from the user (web user).
I need to have a query which will use that function and return the data from the database order by distance from the user [Using { get_distance ($person_location) } function of PHP].
Can anyone help me please?
Upvotes: 0
Views: 75
Reputation: 1236
You can't sort your SQL results on the serverside by the result of a PHP function.*
There are two approaches to your general problem:
Your distance computation probably relies on geo-coordinates (latitude and longitude). Save this data for every address in the database and then do the distance computation in SQL as well.
Find more on how to do this in MySQL here: Fastest Way to Find Distance Between Two Lat/Long Points
Your todo list for this. Do the following things ONCE:
Do the following things from now on:
SELECT
query which computes the distance and does the sortingQuery your database for all addresses, put them into a PHP array, compute the distance to the current user with your function and then sort your array.
I strongly suggest not to do that, however, and implement everything on the server-side (Approach 1).
* well in theory you could, by calulcating the distance for every address offline, updating a temporary table with the result, and then querying your table again using this temporary table to sort your results. However, this is even worse than doing everything in PHP, you shouldn't even consider this!
Upvotes: 2
Reputation: 136
imho it is not possible to use PHP functions in your query, only thing like aggregate functions served by MySQL. I guess you need to process through the data by PHP.
Upvotes: 1