This page contains the following error: error on line 1 at column 1: Document is empty

房东的猫 提交于 2019-12-24 00:59:39

问题


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. 

回答1:


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();


来源:https://stackoverflow.com/questions/36747607/this-page-contains-the-following-error-error-on-line-1-at-column-1-document-is

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!