[英]Why am I not getting XML output from this php file?
我应该从下面的php文件中获取XML输出,该文件正在访问数据库中的信息。
这是php文件:
<?php
require("phpsqlajax_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 ($server, $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)){
// 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>';
?>
这是我应该获得的XML链接:
http://code.google.com/apis/earth/articles/phpsqlearth.html
这是我没有从中获得输出的网站链接:
我认为它返回的很好,除非您在空白页面中选择“查看源代码”,否则它可能在您的浏览器中不可见。
尝试在header()
调用之后立即修改代码,如下所示:
// Start XML file, echo parent node
echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
echo '<markers>';
哼,几句注释可以改善您的代码
1- parseToXML可以由本机htmlspecialchars函数代替。
http://php.net/manual/en/function.htmlspecialchars.php
2-选择*从标记WHERE 1
不需要WHERE 1,只需要SELECT * FROM标记。 不过滤时不需要在哪里
OK,要回答您的问题,请尝试从mysql_fetch_assoc中删除@ in。 您可能会看到一个错误出现。 这可能是导致脚本无法正确执行的原因。
希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.