How can export mysql data in to xml using php
<p>Below code is for export data from mysql table as xml file. I have tried several code but not getting the result. Please check and help me.</p>

<p><strong>Currently getting result is</strong></p>

<pre><code>8sarathsarathernakulam423432washington9rahulrahulernakulam21212121newyork10aaaa3london11bbbb1newyork12cccc2washington13dddd3london
</code></pre>

<p><strong>Code</strong></p>

<pre><code>&lt;?php
require_once "classes/dbconnection-class.php";
if(isset($_POST['export'])){
    header('Content-type: text/xml');
    $xml          = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;";
    $root_element = "addressbook"; //fruits
    $xml         .= "&lt;$root_element&gt;";
    $query        = "SELECT AB.id, AB.name, AB.firstname, AB.street, AB.zipcode, AB.city_id, CI.city FROM address_book AS AB INNER JOIN city AS CI ON AB.city_id = CI.id";
    $result      = $mysqli-&gt;query($query);
    if (!$result) {
        die('Invalid query: ' . $mysqli-&gt;error());
    }

    while($result_array = $result-&gt;fetch_assoc()){
        $xml .= "&lt;address&gt;";
        foreach($result_array as $key =&gt; $value)
        {
            //$key holds the table column name
            $xml .= "&lt;$key&gt;";

            //embed the SQL data in a CDATA element to avoid XML entity issues
            $xml .= "&lt;![CDATA[$value]]&gt;";

            //and close the element
            $xml .= "&lt;/$key&gt;";
        }

        $xml.="&lt;/address&gt;";
    }
    $xml .= "&lt;/$root_element&gt;";
    header ("Content-Type:text/xml");
    //header('Content-Disposition: attachment; filename="downloaded.xml"');
    echo $xml;
}
?&gt;
</code></pre>

<p><strong>Browser shows</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;addressbook&gt;&lt;address&gt;&lt;id&gt;&lt;![CDATA[8]]&gt;&lt;/id&gt;&lt;name&gt;&lt;![CDATA[sarath]]&gt;&lt;/name&gt;&lt;firstname&gt;&lt;![CDATA[sarath]]&gt;&lt;/firstname&gt;&lt;street&gt;&lt;![CDATA[ernakulam]]&gt;&lt;/street&gt;&lt;zipcode&gt;&lt;![CDATA[42343]]&gt;&lt;/zipcode&gt;&lt;city_id&gt;&lt;![CDATA[2]]&gt;&lt;/city_id&gt;&lt;city&gt;&lt;![CDATA[washington]]&gt;&lt;/city&gt;&lt;/address&gt;&lt;address&gt;&lt;id&gt;&lt;![CDATA[9]]&gt;&lt;/id&gt;&lt;name&gt;&lt;![CDATA[rahul]]&gt;&lt;/name&gt;&lt;firstname&gt;&lt;![CDATA[rahul]]&gt;&lt;/firstname&gt;&lt;street&gt;&lt;![CDATA[ernakulam]]&gt;&lt;/street&gt;&lt;zipcode&gt;&lt;![CDATA[2121212]]&gt;&lt;/zipcode&gt;&lt;city_id&gt;&lt;![CDATA[1]]&gt;&lt;/city_id&gt;&lt;city&gt;&lt;![CDATA[newyork]]&gt;&lt;/city&gt;&lt;/address&gt;&lt;address&gt;&lt;id&gt;&lt;![CDATA[10]]&gt;&lt;/id&gt;&lt;name&gt;&lt;![CDATA[a]]&gt;&lt;/name&gt;&lt;firstname&gt;&lt;![CDATA[a]]&gt;&lt;/firstname&gt;&lt;street&gt;&lt;![CDATA[a]]&gt;&lt;/street&gt;&lt;zipcode&gt;&lt;![CDATA[a]]&gt;&lt;/zipcode&gt;&lt;city_id&gt;&lt;![CDATA[3]]&gt;&lt;/city_id&gt;&lt;city&gt;&lt;![CDATA[london]]&gt;&lt;/city&gt;&lt;/address&gt;&lt;address&gt;&lt;id&gt;&lt;![CDATA[11]]&gt;&lt;/id&gt;&lt;name&gt;&lt;![CDATA[b]]&gt;&lt;/name&gt;&lt;firstname&gt;&lt;![CDATA[b]]&gt;&lt;/firstname&gt;&lt;street&gt;&lt;![CDATA[b]]&gt;&lt;/street&gt;&lt;zipcode&gt;&lt;![CDATA[b]]&gt;&lt;/zipcode&gt;&lt;city_id&gt;&lt;![CDATA[1]]&gt;&lt;/city_id&gt;&lt;city&gt;&lt;![CDATA[newyork]]&gt;&lt;/city&gt;&lt;/address&gt;&lt;address&gt;&lt;id&gt;&lt;![CDATA[12]]&gt;&lt;/id&gt;&lt;name&gt;&lt;![CDATA[c]]&gt;&lt;/name&gt;&lt;firstname&gt;&lt;![CDATA[c]]&gt;&lt;/firstname&gt;&lt;street&gt;&lt;![CDATA[c]]&gt;&lt;/street&gt;&lt;zipcode&gt;&lt;![CDATA[c]]&gt;&lt;/zipcode&gt;&lt;city_id&gt;&lt;![CDATA[2]]&gt;&lt;/city_id&gt;&lt;city&gt;&lt;![CDATA[washington]]&gt;&lt;/city&gt;&lt;/address&gt;&lt;address&gt;&lt;id&gt;&lt;![CDATA[13]]&gt;&lt;/id&gt;&lt;name&gt;&lt;![CDATA[d]]&gt;&lt;/name&gt;&lt;firstname&gt;&lt;![CDATA[d]]&gt;&lt;/firstname&gt;&lt;street&gt;&lt;![CDATA[d]]&gt;&lt;/street&gt;&lt;zipcode&gt;&lt;![CDATA[d]]&gt;&lt;/zipcode&gt;&lt;city_id&gt;&lt;![CDATA[3]]&gt;&lt;/city_id&gt;&lt;city&gt;&lt;![CDATA[london]]&gt;&lt;/city&gt;&lt;/address&gt;&lt;/addressbook&gt;
</code></pre>