The query below is currently within a loop(in PHP) that takes latitude, longitude, and id inputs from 3 different arrays. For each position/key in the array the nearest geographical place is selected from the table using the query below.
As the size of the array is typically around 500, so 500 sets of inputs, for which a nearest geographical position needs to be found it is taking some time.
My question: Is there any way to use one query using the input arrays from php at once, and selecting a set of all the nearest geographical positions for each array key?
$sql="SELECT
id, d, tolon, tolat, du,
( 3959 * acos( cos( radians($lat1) )
* cos( radians( travel.tolat ) )
* cos( radians( travel.tolon) - radians($lon1) )
+ sin( radians($lat1) )
* sin( radians( travel.tolat ) ) ) ) AS distance
FROM travel
WHERE userid = $id AND tolon > $lonlow AND tolon < $lonhigh AND tolat > $latlow AND tolat < $lathigh
having distance < $maxdistance ORDER BY distance
";