我正在尝试将带有php的MySQL表数据放入某种Google地图可用的xml文件中。
尝试研究也使我想到了这个问题:I keep getting this error, XML Parsing error: syntax error but still the website runs fine
原始代码来自Google Maps Platform文档指南部分,该部分介绍了将MySQL和PHP与Maps结合使用。有三种变体,我什么都无法工作。
使用的变体可以在这里找到:https://developers.google.com/maps/documentation/javascript/mysql-to-maps#echoxml
当前使用:
我尝试使用的代码如下:
<?php
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
$username = "username";
$password = "password";
$database = "db";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db($database,$dbh)
or die("Could not select database $database");
print "Connected to MySQL database $database";
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// this echo "waypoint"; was removed. It was only there to help me find out how far the code executed.
echo "waypoint";
header("Content-type: text/xml");
// Start XML file, echo parent node
echo "<?xml version='1.0' ?>";
echo '<markers>';
$ind=0;
// Iterate through the rows, printing XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
// Add to XML document node
echo '<marker ';
echo 'id="' . $row['id'] . '" ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo '/>';
$ind = $ind + 1;
}
// End XML file
echo '</markers>';
?>
我当前收到此错误:
XML Parsing Error: syntax error
Location: ... /phpFILE.php
Line Number 1, Column 1:
当我在浏览器中运行文件并“查看页面源代码”时,得到以下输出:
Connected to MySQL database [db name deleted by anderopa] <?xml version='1.0' ?><markers></markers>