PHP - mySQL query issue
I'm finding the nearest location (with long/lat from Google API) in a radius area with lon/lat in my SQL database, here's my query:
$query = "SELECT id, ( 3959 * acos( cos( radians(".$lat.") ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(".$long.") ) + sin( radians(".$lat.") ) * sin( radians( lat ) ) ) ) AS distance FROM finder_location HAVING distance < 25 ORDER BY distance LIMIT 0 , 1";
$result = mysql_query( $query );
$row = mysql_fetch_array($result);
echo $row['id'];
The query is returning correctly inside phpMyAdmin as:
id 9
distance 0.74066713768542
So, the query is compiling correctly...
But, when I tried to output in PHP, I get these errors:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '>) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(-73.988307 ) )' at line 1 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
开发者_JAVA百科
Your php $lat
variable is not populated correctly. You can see the trailing end of an html tag in the mysql error. Debug your $lat
and $long
to make sure they have the correct values before the query.
精彩评论