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

创建一个XML文档,以便使用PHP、MySQL和;谷歌地图,php,mysql,google-maps,Php,Mysql,Google Maps,我使用以下内容创建一个带有PHP、MySQL和Google地图的商店定位器: 到目前为止,我的代码如下: <?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

我使用以下内容创建一个带有PHP、MySQL和Google地图的商店定位器:

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

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

?>
有谁能帮我弄明白这一点或给我指出正确的方向吗?感谢您的帮助


感谢前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输出():


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

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

我以前见过这样的东西被添加到web地址中,但我不知道它在做什么(每天都是学校的一天…!)

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

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

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

您是否在URL中传递lat、lng和radius参数?不,这是我最困惑的地方。我当时并不认为我需要这样做,因为我现在只想创建一个XML文档,因为这是第一步。我不完全确定这些应该如何在URL中传递?与其硬编码这些值,您可能应该在适当的情况下将
$\u GET
$\u POST
变量编码到您的答案中。
http://localhost/user/xml_output.php?lat=49.860980&lng=0.681800&radius=7