XML developed from MYSQLi using PHP


I am developing an XML output from MYSQL using MYSQLi and PHP.

I have the code below.

include (“dbconn.php”);

@$db = new mysqli( $DB_HOST, $DB_NAME, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
echo ‘error connecting to db’;
$query = “SELECT o.customerNumber, o.orderNumber, o.orderDate, od.quantityOrdered , od.orderLineNumber
FROM orders AS o, orderdetails AS od WHERE o.orderNumber = od.orderNumber
and customerNumber =‘103’
order by o.customerNumber” ;
$result = mysqli_query($db,$query);

$xml = new DOMDocument('1.0' , 'ISO-8859-1');
$xml-&gt;formatOutput = true;

if(mysqli_num_rows($result)) {

    while($row = mysqli_fetch_assoc($result)) {
        $r = $xml-&gt;createElement( "Orders" );
        foreach($row as $field=&gt;$value) {
            $tChild = $xml-&gt;createElement( $field );
            $tChild-&gt;appendChild( $xml-&gt;createTextNode($value) );
            $r-&gt;appendChild( $tChild );     
    $xml-&gt;appendChild( $r );
    echo $xml-&gt;saveXML();


and it returns what looks like XML , but when I read the write.xml file it says its Junk.
I think its because I am not declaring enough parent nodes.

Please advise.

Many thanks