问题描述
我无法找到解决方案,请帮忙.下面是代码.提前致谢
query($sql);$xml = new SimpleXMLElement('<xml/>');如果($result->num_rows>0){//输出每一行的数据while($row = $result->fetch_assoc()) {$mydata = $xml->addChild('mydata');$mydata->addChild('Id',$row['idProjet']);}} 别的 {echo "0 结果";}$conn->close();标头(内容类型:文本/xml");echo($xml->asXML());?>
和文件connect.php
$servername = "localhost";$用户名 = "root";$密码 = "";$dbname = "mtocrowdrise";//创建连接$conn = new mysqli($servername, $username, $password, $dbname);//检查连接如果($conn->connect_error){die("连接失败:" . $conn->connect_error);}echo "连接成功";
与此同时,我不断收到此错误:
此页面包含以下错误:第 1 行第 1 列错误:文档为空下面是页面渲染到第一个错误.
当页面被转换为 XML 文档 (header ("Content-Type:text/xml");
).
从connect.php
中删除echo "connected succesfully";
.
如果出现以下情况,您(最终)也会遇到相同的错误:
...} 别的 {echo "0 结果";}...标头(内容类型:文本/xml");满足.因此,只有在没有错误并且实际上有一些 XML 可以显示的情况下,您才应该将文档转换为 XML.
如果有结果要显示(根据您的原始代码),则类似以下内容只会将文档设置为 XML:
require_once('connect.php');$sql = "从项目中选择 *";$result = $conn->query($sql);如果($result->num_rows>0){$xml = new SimpleXMLElement('<xml/>');//输出每一行的数据while($row = $result->fetch_assoc()) {$mydata = $xml->addChild('mydata');$mydata->addChild('Id',$row['idProjet']);}标头(内容类型:文本/xml");echo($xml->asXML());} 别的 {echo "0 结果";}$conn->close();
Im unable to find out the solution , please help. below is the code. Thanks in advance
<?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());
?>
and the file 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";
Meanwhile i keep getting this error:
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.
You shouldn't be writing/outputting any HTML to the page when said page is being cast as an XML Document (header ("Content-Type:text/xml");
).
Remove echo "connected succesfully";
from connect.php
.
You'll also (eventually) get the same error if:
...
} else {
echo "0 results";
}
...
header ("Content-Type:text/xml");
satisfies. So you should only cast the document to XML if there are no errors and there is actually some XML to display.
Something like the following would only set the Document to XML if there are results to display (per your original code):
require_once('connect.php');
$sql = "select * from projet";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$xml = new SimpleXMLElement('<xml/>');
// output data of each row
while($row = $result->fetch_assoc()) {
$mydata = $xml->addChild('mydata');
$mydata->addChild('Id',$row['idProjet']);
}
header ("Content-Type:text/xml");
echo($xml->asXML());
} else {
echo "0 results";
}
$conn->close();
这篇关于此页面包含以下错误:第 1 行第 1 列错误:文档为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!