问题
I am using the creating store locator with PHP, MySQL and Google Maps example provided at https://developers.google.com/maps/articles/phpsqlsearch_v3.
I am having issues getting the XML doc to write. I can run the query successful from Querious(DB app), but when attempting to run the provided script, which I modified to mysqli, things aren't working out for me. I am getting nothing returned. The DB is set up as supplied in the example. Here is the modified PHP that I use:
PHP
<?php
require_once('../includes/connection.php');
require_once('../includes/function.php');
// Get parameters from URL
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Search the rows in the markers table
$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",
mysqli_real_escape_string($mysqli , $center_lat),
mysqli_real_escape_string($mysqli , $center_lng),
mysqli_real_escape_string($mysqli , $center_lat),
mysqli_real_escape_string($mysqli , $radius));
$result = mysqli_query($mysqli, $query);
$result = mysqli_query($mysqli, $query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name", $row['name']);
$newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("distance", $row['distance']);
}
echo $dom->saveXML()
?>
I know that both my connection are successful and that functions is error free. Thanks for any insight on this.
回答1:
I wouldn't be so sure about "error free" :)
You are using the mysql_fetch_assoc() function (part of the deprecated mysql_* extensions) with mysqli, which are not compatible. The @ before this method call is hiding the error that you would normally see from the incorrect fetch of the data as an associative array.
If you want to get an associative array with mysqli, you should use:
$array = $result->fetch_assoc();
http://php.net/manual/en/mysqli-result.fetch-assoc.php
http://us3.php.net/manual/en/language.operators.errorcontrol.php
来源:https://stackoverflow.com/questions/13225980/google-map-api-php-example