【发布时间】:2016-04-20 14:52:47
【问题描述】:
我无法找到解决方案,请帮助。下面是代码。提前致谢
<?php
require_once('connect.php');
$sql = "select * from projet";
$result = $conn->query($sql);
$xml = new SimpleXMLElement('<xml/>');
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$mydata = $xml->addChild('mydata');
$mydata->addChild('Id',$row['idProjet']);
}
} else {
echo "0 results";
}
$conn->close();
header ("Content-Type:text/xml");
echo($xml->asXML());
?>
还有文件connect.php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mtocrowdrise";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "connected succesfully";
同时我不断收到此错误:
This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.
【问题讨论】:
-
从
connect.php中删除echo "connected succesfully";。当所述页面被转换为 na XML 文档 (header ("Content-Type:text/xml");) 时,您不应该向页面写入/输出任何 HTML。如果} else { echo "0 results";满足,您(最终)也会得到同样的错误。因此,只有在没有错误并且实际上有一些 XML 要显示的情况下,您才应该将文档转换为 XML。 -
非常感谢@Marcus