使用PHP将mySQL表输出为XML,以便在google地图上绘制位置
我已经设法使用PHP和XML输出了一个表来绘制google地图,但是我很难连接到表以获得相同的结果,下面是PHP代码:使用PHP将mySQL表输出为XML,以便在google地图上绘制位置,php,mysql,xml,Php,Mysql,Xml,我已经设法使用PHP和XML输出了一个表来绘制google地图,但是我很难连接到表以获得相同的结果,下面是PHP代码: <?php // Start XML file, create parent node $dom = new DOMDocument("1.0"); $node = $dom->createElement("markers"); $parnode = $dom->appendChild($node); // Opens a connection t
<?php
// 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=mysql_connect ("localhost", "root", "z2f2w3k8") or die(mysql_error());
mysql_select_db("zena2") or die(mysql_error());
// Set the active MySQL database
$db_selected = mysql_select_db("zena2");
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT customer.customerNo, customer.firstName, customer.lastName, customer.houseNum, customer.address, customer.telephone, map.customerNo, map.lat, map.long".
"FROM customer, map".
"WHERE (customer.customerNo = map.customerNo)";
$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)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name",$row['firstName'+'lastName']);
$newnode->setAttribute("address", $row['houseNum'+'address']);
$newnode->setAttribute("phone", $row['telephone']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['long']);
$newnode->setAttribute("type", $row['customerNo']);
}
echo $dom->saveXML();
?>
此错误消息是什么意思?您的SQL查询不正确。在SQL查询行上最后一个引号前添加空格 因此,您的代码应该如下所示:
$query = "SELECT customer.customerNo, customer.firstName, customer.lastName, customer.houseNum, customer.address, customer.telephone, map.customerNo, map.lat, map.long ".
"FROM customer, map ".
"WHERE (customer.customerNo = map.customerNo)";
此外,您还可以删除此行,因为您要选择数据库两次:
mysql_select_db("zena2") or die(mysql_error());
这一行的格式错误:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api /js?sensor=false"></script>
您应该已经阅读了错误消息的哪一部分您很难理解?好的,谢谢。xml输出现在按预期工作,但它没有绘制google地图。有什么想法吗?检查Javascript控制台是否有错误。因为Firefox会给你很多帮助。可能是php文件名错误-请尝试绝对路径。再次感谢您的帮助,firebug发现了一个错误,但我不知道它告诉我什么。我在阅读了google开发者教程后解决了这个问题,再次感谢
mysql_select_db("zena2") or die(mysql_error());
<script type="text/javascript" src="http://maps.googleapis.com/maps/api /js?sensor=false"></script>