I’m trying to select businesses from my database who fall within a 10mi radius of the user. Testing shows that the problem lies in my query (though no MySQL error is thrown). The variables $lat and $lng are imported from Javascript and they, too, are fine. I suspect the issue is something to do with my use of BETWEEN, as this is the first time I’ve used it. Any ideas?
$r = 20; // in miles
$lat_min = $lat - ($r / 69);
$lat_max = $lat + ($r / 69);
$lng_min = $lng - $r / abs(cos(deg2rad($lat)) * 69);
$lng_max = $lng + $r / abs(cos(deg2rad($lat)) * 69);
$query = "SELECT name FROM businesses WHERE $lat BETWEEN $lat_min AND $lat_max $lng BETWEEN $lng_min AND $lng_max ORDER BY Rand() LIMIT 6";
The query should be:
Somehow $lat does not look like a column name in the table. Also you were missing an AND.