MySQL Syntax Error in Haversine Formula
I'm getting a frustrating MySql syntax error in my code below. The actual error is:
SQLSTATE[42000]: Syntax error or access violation: 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 ' JOIN storetable ON pricelist.storecode = storetable.storecode JOIN itemlist ON' at line 9"
Addition开发者_如何学Pythonal info: This code based on Google Maps Php/MySql example: http://code.google.com/apis/maps/articles/phpsqlsearch.html
I was able to get this code to work fine before I tried to add the JOIN statements.
$query = sprintf("SELECT storetable.storeaddress,
storetable.storename,
storetable.lat,
storetable.lng,
( 3959 * acos( cos( radians('%s') ) *
cos( radians( storetable.lat ) ) * cos(radians(storetable.lng) - radians('%s') ) +
sin( radians('%s') ) * sin( radians( storetable.lat ) ) ) ) AS distance
FROM pricelist
HAVING distance < 25
ORDER BY distance
LIMIT 0 , 20,
JOIN storetable ON pricelist.storecode = storetable.storecode
JOIN itemlist ON pricelist.upccode = itemlist.upccode",
mysql_real_escape_string($latitude),
mysql_real_escape_string($longitude),
mysql_real_escape_string($latitude));
JOINs were in the wrong place - they come after the FROM clause, before the WHERE clause - Use:
$query = sprintf("SELECT storetable.storeaddress,
storetable.storename,
storetable.lat,
storetable.lng,
( 3959 * acos( cos( radians('%s') ) *
cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) +
sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance
FROM pricelist
JOIN storetable ON pricelist.storecode = storetable.storecode
JOIN itemlist ON pricelist.upccode = itemlist.upccode
HAVING distance < 25
ORDER BY distance
LIMIT 0, 20",
mysql_real_escape_string($latitude),
mysql_real_escape_string($longitude),
mysql_real_escape_string($latitude));
精彩评论