Blog > detail

Just my tiny mind

Export MySql Into XML

26-2-2012 Admin General 2 Comments

Export MySql Into XML

Previously I just want to export several tables of my database from one server to another server. Let say, I want to send tables from server A to server B. In this case, I just make a PHP file that will generate XML file when executed. In this case, the PHP file to Export MySql Into XML will be placed in server A.


Here the codes:


//connecting to DB

$host = 'localhost';//host name, in this case we use localhost

$user = 'root';//user name, in this case we use root

$pass = 'password';//password, in this case we use password

$conn = mysql_connect($host, $user, $pass)

or die('Connection Failed');//will show "Connection Failed" if the connection failed

$db_name = 'member';//declare the database that will be used

mysql_select_db($db_name);//selecting the data base


//tables that will be exported

$tables = array("client","ib");


header('Content-type: text/xml');

header('Content-Disposition: attachment; filename="'.$table.'.xml"');


$text ='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<'.$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</$key>";

}

}

$text .= "

</$table>";

}


$text .= "

</table_$table>";

}

$text .= "

</$db_name>";

echo $text;

mysql_close($connection);


When the file is executed, the file will generate an XML file. I will post how to import database from an XML file next time. Hope this simple post can help you alot.

Export MySql Into XML Export MySql Into XML Export MySql Into XML

Export MySql Into XML Export MySql Into XML Export MySql Into XML

Export <a href=MySql Into XML" width="1" height="1" />

word word

Export MySql Into XML

Export MySql Into XML

Export MySql Into XML