I’m doing an android application. It has a server back end too. I need to select latitude and longitude from database which comes in a bounding square or circle. I am using the below code.. But it is not working. Please help me..
<?php
$lat = $_GET['lat'];
$lon = $_GET['lon'];
$rad = 10;
$R = 6371;
$maxLat = $lat + rad2deg($rad/$R);
$minLat = $lat - rad2deg($rad/$R);
$maxLon = $lon + rad2deg($rad/$R/cos(deg2rad($lat)));
$minLon = $lon - rad2deg($rad/$R/cos(deg2rad($lat)));
$lat = deg2rad($lat);
$lon = deg2rad($lon);
$con=mysql_connect("localhost","root","");
mysql_select_db("circle",$con);
$sql="Select name,acos(sin($lat)*sin(radians(`lat`))+cos($lat)*cos(radians(`lat`))*cos(radians(`long`)-$lon))*$R As D From circle Where acos(sin($lat)*sin(radians(`lat`))+cos($lat)*cos(radians(`lat`))*cos(radians(`long`)-$lon))*$R < $rad";
$rslt= mysql_query($sql);
$row=mysql_num_rows($rslt);
$arr=mysql_fetch_assoc($rslt);
print_r($arr);
?>
Thanks
Since
longis a reserved word, you can’t use it directly as a column name. (Are you sure your column isn’t nameloninstead?)If the column is named
longyou can escape it using backticks:That way MySQL knows you’re using a column name and it doesn’t try to interpret the text as something else.
Update
The new query from the OP with sorting by distance. (I did rename D to
distance.)Please note that this query will be slow because it cannot use indexes to limit the number of rows nor for sorting. It will have to calculate the distance for every row before it can determine whether or not the row should be in the result set. That won’t really matter if your table contains 5 rows, but it will matter if there are 500,000 rows.