I'm trying to build a store locator for gas stations with google maps api. I've done everything as described here (http://code.google.com/apis/maps/articles/phpsqlsearch.html), but
- The search for address function does not recognize most of the input (I can live with that)
- The SQL statement as provided by google does not return any results (I've checked that by substituting the statement with "
SELECT * FORM marke开发者_如何学运维rs
", which works)
My DB table looks like
ID
NAME
ADDRESS
LAT
LNG
and the statement I'm having troubles with is:
$query = sprintf("SELECT name, address, lat, lng ( 6371 * 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 , 5",
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);
which gets its values from the address search.
Does anybody have experience with this or a similar problem?
edit: found the first error: i missed out a comma between lng and (6371... if I insert this directly in phpmyadmin (substituting '%s' with real values) i get the result i want, so I suppose the problem is passing the variables into SQL; hope that helps narrowing it down.
You need to pass in floats instead of strings, so your php should be
$query = sprintf("SELECT name, address, lat, lng, ( 6371 * acos( cos( radians(%f) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(%f) ) + sin( radians(%f) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < %d ORDER BY distance LIMIT 0 , 5",
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);
精彩评论