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