使用PHP生成XML时的奇怪行为
我在php/mysql中遇到了一些非常奇怪的行为。我目前有一个包含名称、地址、lat、lng和类型值的位置数据库 我使用以下代码从数据库生成xml使用PHP生成XML时的奇怪行为,php,mysql,xml,Php,Mysql,Xml,我在php/mysql中遇到了一些非常奇怪的行为。我目前有一个包含名称、地址、lat、lng和类型值的位置数据库 我使用以下代码从数据库生成xml <?php require("dbinfo.php"); function parseToXML($htmlStr) { $xmlStr=str_replace('<','<',$htmlStr); $xmlStr=str_replace('>','>',$xmlStr);
<?php
require("dbinfo.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;
}
// Opens a connection to a MySQL server
$connection=mysql_connect (localhost, $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());
}
// 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());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result))
{
if($row['lat'] != 0 && $row['lng'] !=0)
{
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo '/>';
}
}
// End XML file
echo '</markers>';
?>
@mysql\u fetch\u assoc($result)禁止sql错误。您可能有一些被忽略的小问题。将其更改为mysql\u fetch\u assoc($result),然后查看发生了什么