我有一个 PHP 文件,它从
读取凭据 require("phpRequireInfo.php");
而且,我的问题是,每当我编译它时,它总是给我错误。我的 php 文件如下所示:
<?php
header("Content-type: text/xml");
require("phpRequireInfo.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 ($database, $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$dbname= 'csuser';
// Set the active MySQL database
$db_selected = mysql_select_db($dbname, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM Bars ";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// 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['Name'] );
}
echo $dom->saveXML();
?>
我收到的错误是:“第 4 行第 6 列错误:仅在文档开头允许 XML 声明”
运行 PHP 文件时,XML 文件如下所示: http://imgur.com/SWCZ8sE
我们将非常感谢您的帮助
最佳答案
为什么一开始有3个空行? 在 require 之前尝试 ob_start() ,在 echo 之前尝试 ob_end_clean() ,以避免它们
ob_start();
require("phpRequireInfo.php");
.
.
.
ob_end_clean();
echo $dom->saveXML();
关于PHP MYSQL XML 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20075638/