我試圖約4不同的嘗試來拉我的數據庫信息,並將其顯示爲一個XML。然而,當我點擊指定的按鈕,它會顯示錯誤:對外輸出數據庫信息XML
error on line 2 at column 1: Document is empty
我的連接工作正常,所以它不是一個問題,但我之前,所以我必須忽視的東西沒有嘗試這樣做。
Generatexml.php
<?php
header('Content-type: text/xml');
include 'connection.php';
$query = "SELECT * FROM users";
$result = mysqli_query($mysqli_conn, $query) or die ("Error in query: $query. ".mysql_error());
//Top of xml file
$_xml .="<people>";
while($row = mysqli_fetch_array($result)) {
$_xml .="<person>";
$_xml .="<firstname>".$row['FirstName']."</firstname>";
$_xml .="<lastname>".$row['LastName']."</lastname>";
$_xml .="<username>".$row['Username']."</username>";
$_xml .="<password>".$row['Password']."</password>";
$_xml .="<id>".$row['P_Id']."</id>";
$_xml .="</person>";
}
$_xml .="</people>";
//Parse and create an xml object using the string
$xmlobj=new SimpleXMLElement($_xml);
//write to an XML file
$xmlobj->asXML('people.xml');
echo $xmlobj->asXML();
?>
編輯:我用另一種方法,我能拉數據庫中的信息,並顯示它,但它不是以XML格式顯示。
<?php
include 'connection.php';
//query the database
$sqlQuery = "SELECT * FROM users";
//run query
if ($result = mysqli_query($db, $sqlQuery)) {
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<results>';
echo '<people>';
//fetch associative array
while ($row = mysqli_fetch_assoc($result)) {
echo '<person>';
echo '<firstname>' . $row["FirstName"] . '</firstname>';
echo '<lastname>' . $row["LastName"] . '</lastname>';
echo '</person>';
}
echo '</people>';
}
//close the database connection
$db->close();
echo '</results>';
?>
爲什麼通過將字符串混合在一起然後使用XML庫將字符串轉換爲DOM然後立即返回到文本來生成XML? – Quentin
我只是跟着導遊,但沒有多少運氣 – user2326995