+91-943-185-6038 me@shashidharkumar.com

MySQLThere are a couple of ways to export data from MySQL to a XML file. This post looks at how to export the data from MySQL into a XML file with PHP.

The example code below uses the raw mysql_* functions but it should be easy enough to substitute a database library’s functions instead. It also writes the data out line by line to the XML file whereas you could buffer the whole file in memory and write it out at one go; however if the resultset is large it may be better to write it out line by line so as not to consume too much memory.

The $server, $login, $password, $db and $table variables should be obvious in their purpose.


<'.$db_name.'>';

foreach($tables as $table){
    $result = mysql_query("SELECT * FROM  `$table`");
    $text .='
    <table_'.$table.'>';

    while($row=mysql_fetch_array($result)) {
        $text .=  "
        <$table>";
            foreach($row as $key => $val){
                if (!preg_match('#^d+$#', $key, $match)){
                    $text .=  "
                    <$key>$val";
                }
            }
        $text .=  "
        ";
    }

    $text .= "
    ";
}
    $text .= "
";
echo $text;
mysql_close($conn);
?>
See also  How to reset Auto-increament in MySQL?