Reputation: 421
I have the following mysql query in my PHP file:
$shopID = mysql_real_escape_string($_GET['shop_id']);
$latitudeCenter = mysql_real_escape_string($_GET['lat_center']);
$longtitudeCenter = mysql_real_escape_string($_GET['lng_center']);
$lat_min = $latitudeCenter - 0.045;
$lat_max = $latitudeCenter + 0.045;
$long_min = $longtitudeCenter - (0.045 / cos($latitudeCenter*M_PI/180);
$long_max = $longtitudeCenter + (0.045 / cos($latitudeCenter*M_PI/180);
$sql = "SELECT * FROM shops WHERE shop_id = '$shopID' AND lat >= '$lat_min' AND lat <= '$lat_max' AND lng >= '$long_min' AND lng <= '$long_max'";
For some reason the query is not running successfully. Is the above query valid? Thanks
EDIT:
There is something wrong with the $long_min and $long_max calculations as when they are commented out, it works ok.
Here is the code I tried to conver to PHP:
lat_min = lat_center - 0.045;
lat_max = lat_center + 0.045;
long_min = long_center - (0.045 / Math.cos(lat_center*Math.PI/180);
long_max = long_center + (0.045 / Math.cos(lat_center*Math.PI/180);
What is wrong with my PHP?
Upvotes: 1
Views: 10194
Reputation: 80653
Use the MySQl's BETWEEN
and NEVER use quotes for comparing numbers. So the query becomes:
$sql = "SELECT *
FROM shops
WHERE shop_id = $shopID
AND lat BETWEEN $lat_min AND $lat_max
AND lng BETWEEN $long_min AND $long_max";
Where, I have considered that shop_id
column is auto-incremental number.
Upvotes: 2