简体   繁体   English

PHP-MySQL查询问题

[英]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: 我在SQL数据库中找到半径为lon / lat的半径区域中最近的位置(使用Google API中的long / lat),这是我的查询:

$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: 查询在phpMyAdmin内部正确返回为:

id  9
distance 0.74066713768542

So, the query is compiling correctly... 因此,查询正在正确编译...

But, when I tried to output in PHP, I get these errors: 但是,当我尝试用PHP输出时,出现以下错误:

You have an error in your SQL syntax; 您的SQL语法有误; 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 检查与您的MySQL服务器版本相对应的手册以在'>))* cos(radians(lat))* cos(radians(lon)-radians(-73.988307))'第1行使用正确的语法警告:mysql_fetch_array ():提供的参数不是有效的MySQL结果资源

Your php $lat variable is not populated correctly. 您的php $lat变量未正确填充。 You can see the trailing end of an html tag in the mysql error. 您可以在mysql错误中看到html标记的结尾。 Debug your $lat and $long to make sure they have the correct values before the query. 调试$lat$long以确保它们在查询前具有正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM