Q:

latitude longitude script gives MySQL error "You have an error in your SQL syntax; ... for the right syntax to use near 'long"

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

A:

Since long is a reserved word, you can't use it directly as a column name. ( Are you sure your column isn't name lon instead?) 
If the column is named long you can escape it using backticks:
$sql = "select name from circle where lat > $minLat and lat < $maxLat and `long` > $minLon and `long` < $maxLon";

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.) 
$sql="SELECT name, acos(sin($lat)*sin(radians(`lat`))+cos($lat)*cos(radians(`lat`))*cos(radians(`long`)-$lon))*$R AS `distance`
    FROM circle
    WHERE `distance` < $rad
    ORDER BY `distance` ASC";

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.