Google Maps Store Locator PHP / MySQL

时间:2014-03-13 14:14:28

标签: php mysql google-maps

我正在尝试按照本教程进行操作:

https://developers.google.com/maps/articles/phpsqlsearch_v3

不幸的是,我的XML输出中一直有空白值:

<Competitors>
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/>
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/>
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/>
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/>
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/>
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/>
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/>
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/>
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/>
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/>
</Competitors>

我在我的数据库中设置了一个类似于教程中显示的表:

Table Screenshot

这是我的生成XML的代码,根据教程:

<?php  
$username="#######";
$password="#######";
$database="#######";

// 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("Competitors");
$parnode = $dom->appendChild($node);

// Opens a connection to a mySQL server
$connection=mysql_connect ("host", $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());
}

// Search the rows in the Competitors 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 Competitors 
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);

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("Competitors");
  $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();
?>

我不确定我在哪里犯了错误 - 非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您需要替换此

// Opens a connection to a mySQL server
$connection=mysql_connect ('host', $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Opens a connection to a mySQL server
$connection=mysql_connect ('localhost', $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}

刚尝试过,我得到了xml

此外,代码示例仅使用localhost而不是"localhost",因此php认为它是常量