创建一个XML文档,以便使用PHP、MySQL&;谷歌地图


Creating an XML document in order to create a Store Locator with PHP, MySQL & Google Maps

我正在使用以下内容创建一个带有PHP、MySQL和amp;谷歌地图:https://developers.google.com/maps/articles/phpsqlsearch_v3

到目前为止,我的代码如下:

<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

require("dbinfo.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);
// Opens a connection to a mySQL server
$connection=mysqli_connect($hostname, $username, $password);
if (!$connection) {
  die("Not connected : " . mysqli_error());
}

// Set the active mySQL database
$db_selected = mysqli_select_db($database, $connection);
if (!$db_selected) {
  die ("Can not use db : " . mysqli_error());
}
// Search the rows in the locations table
$query = sprintf("SELECT id, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM locations HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
  mysqli_real_escape_string($center_lat),
  mysqli_real_escape_string($center_lng),
  mysqli_real_escape_string($center_lat),
  mysqli_real_escape_string($radius));
$result = mysqli_query($query);
$result = mysqli_query($query);
if (!$result) {
  die("Invalid query: " . mysqli_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
  $node = $dom->createElement("location");
  $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();
?>

当我运行这个时,我得到以下错误:

    Notice: Undefined index: lat in /customers/b/7/e/tournamentfinder.co.uk/httpd.www/test/genxml.php on line 11
 Notice: Undefined index: lng in /customers/b/7/e/tournamentfinder.co.uk/httpd.www/test/genxml.php on line 12
 Notice: Undefined index: radius in /customers/b/7/e/tournamentfinder.co.uk/httpd.www/test/genxml.php on line 13
 Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /customers/b/7/e/tournamentfinder.co.uk/httpd.www/test/genxml.php on line 28
 Warning: mysqli_error() expects exactly 1 parameter, 0 given in /customers/b/7/e/tournamentfinder.co.uk/httpd.www/test/genxml.php on line 30 Can not use db :

有人能帮助我理解这一点吗?或者为我指明正确的方向吗?感谢您的帮助!

感谢

前3条错误消息:

Notice: Undefined index: lat in /customers/b/7/e/tournamentfinder.co.uk/httpd.www/test/genxml.php on line 11
Notice: Undefined index: lng in /customers/b/7/e/tournamentfinder.co.uk/httpd.www/test/genxml.php on line 12
Notice: Undefined index: radius in /customers/b/7/e/tournamentfinder.co.uk/httpd.www/test/genxml.php on line 13

表示您需要通过";"合理";查询字符串中lat/lng/radius的值(例如?lat=37&lng=-122&radius=25000),如您参考的教程中所述:

检查XML输出是否有效

从浏览器中调用这个PHP脚本,以确保它生成有效的XML。如果您怀疑连接到数据库有问题,如果删除文件中将标头设置为text/xml内容类型的行,则可能会发现调试更容易,因为这通常会导致浏览器尝试解析xml,并可能使您难以看到调试消息。

如果脚本工作正常,并且您将合理的示例查询参数附加到URL(例如?lat=37&lng=-122&radius=25),您应该看到这样的XML输出(phpsqlsearch_expectedoutput.XML):

作为编码的新手,我也对此感到困惑,但答案很简单:

当你在浏览器中导航到PHP文件的URL时,你所要做的就是在它的末尾添加?lat=37&lng=-122&radius=25之类的东西。这使得PHP脚本使用这些参数,将它们放入SQL查询中,在那里您可以看到'%s'占位符,然后搜索表,并给出查询的答案。

我以前见过这样的东西添加到网址上,但不知道它在做什么(每天都是学生日…!)

作为参考,我在浏览器中的URL是:

http://localhost/user/xml_output.php?lat=49.860980&lng=0.681800&radius=7

因为我正在使用我自己的位置数据。