我正试图从谷歌这个例子中使用我的代码。
https://developers.google.com/maps/articles/phpsqlsearch_v3
我正在使用Meekro PHP MySQL库,因为它有助于我正在进行的其他查询。
这是给我错误的部分。
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));
$result = mysql_query($query);
我正在使用以下代码:
$results = DB::query("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
$center_lat,
$center_lng,
$center_lat,
$radius);
我总是使用以下参数获得以下错误:
?lat=37&lng=-122&radius=25
#1064 - 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 '37'') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(''-122'') ) + si' at line 1
即使我尝试在phpMyAdmin中运行查询,我也会遇到同样的错误。
答案 0 :(得分:1)
删除引号arround%s:
$results = DB::query("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians(%s) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(%s) ) + sin( radians(%s) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < %s ORDER BY distance LIMIT 0 , 20",
$center_lat,
$center_lng,
$center_lat,
$radius);