生成XML的PHP脚本中的编码错误
从MySQL数据库生成XML的PHP文件有问题。代码如下生成XML的PHP脚本中的编码错误,php,mysql,xml,encoding,Php,Mysql,Xml,Encoding,从MySQL数据库生成XML的PHP文件有问题。代码如下 <?php require("decibelone_dbinfo.php"); function parseToXML($htmlStr) { $xmlStr=str_replace('<','<',$htmlStr); $xmlStr=str_replace('>','>',$xmlStr); $xmlStr=str_replace('"','"'
<?php
require("decibelone_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 location 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)){
// ADD TO XML DOCUMENT NODE
echo '<marker>';
echo '<name>' . parseToXML($row['name']) . '</name>';
echo '<address>' . parseToXML($row['address']) . '</address>';
echo '<latitude>' . $row['latitude'] . '</latitude>';
echo '<longitude>' . $row['longitude'] . '</longitude>';
echo '<description>' . $row['description'] . '</description>';
echo '<time>' . $row['time'] . '</time>';
echo '</marker>';
}
// End XML file
echo '</markers>';
?>
您可以使用DomDocument类。我认为你用的是一种老办法。请点击以下链接
“我不确定问题出在哪里…”我也不知道–错误出在哪里?您能在这里发布错误消息并生成xml文件吗。只需要看看是哪些角色造成了问题。