For a lot of time, we need to save the data to other places, such as xml or to save the xml data to the mysql database, next we provide a conversion program between phpmysql and xml data. For a lot of time, we need to save the data to other places, such as xml or to save the xml data to the mysql database, below we provide a php mysql and xml Data Conversion Program.
Script ec (2); script
Let's take a look at the call method.
$ Xml = new MySQL2XML (array ('host' => 'localhost', 'username' => 'root', 'Password' => '', 'database' => 'mysql '));
$ Xml-> setTables (array ('wp _ term_relationships ', 'wp _ terms'); // you can specify a backup table.
$ Xml-> setSaveFolder ('datas/'); // Save the folder of the backup file
$ Xml-> toXML (); // backup start
?>
The following is the type file of the Conversion Program.
Class MySQL2XML {
Protected $ conn;
Protected $ result;
Protected $ tables;
Protected $ saveFolder = 'datas /';
Public function _ construct ($ config = NULL ){
If ($ config! = NULL & is_array ($ config )){
$ This-> connect ($ config );
}
}
Public function connect ($ config ){
$ This-> conn = mysql_connect ($ config ['host'], $ config ['username'], $ config ['Password']);
If ($ this-> conn ){
Mysql_select_db ($ config ['database']);
Return true;
}
Return false;
}
Public function setSaveFolder ($ folder ){
If (is_dir ($ folder )){
$ This-> saveFolder = rtrim (str_replace ("\", "/", $ folder ),'/');
Return true;
}
Return false;
}
Public function setTables ($ tables ){
If (is_array ($ tables )){
$ This-> tables = $ tables;
Return true;
}
Return false;
}
Public function query ($ query ){
If (! Isset ($ query) | trim ($ query) = '') return false;
$ This-> result = mysql_query ($ query );
If ($ this-> result) return true;
Return false;
}
Public function toXML (){
If (! Isset ($ this-> tables) return false;
Foreach ($ this-> tables as $ table ){
$ File = $ this-> saveFolder. $ table. '. xml ';
$ Fp = @ fopen ($ file, 'w ');
If (! $ Fp) exit ('can not write file ');
Fwrite ($ fp, $ this-> tableToXML ($ table ));
Fclose ($ fp );
Unset ($ fp );
}
Return true;
}
Public function tableToXML ($ table ){
Header ("content-type: text/xml; charset = UTF-8 ");
$ Xml =" N N ";
$ Fields = $ this-> getFields ($ table );
$ Datas = $ this-> getDatas ($ table );
$ Cdata = array ();
Foreach ($ datas as $ data ){
Foreach ($ data as $ key => $ value)
$ Cdata [$ key] [] = $ value;
}
Foreach ($ fields as $ element ){
$ Xml. = "t N ";
Foreach ($ cdata [$ element ['field'] as $ value ){
$ Xml. = "tt {$ Value}N ";
}
$ Xml. = "t N ";
}
$ Xml. =' ';
Return $ xml;
}
Protected function getFields ($ table ){
$ Query = "show fields from {$ table }";
$ This-> query ($ query );
Return $ this-> fetchAll ();
}
Protected function getDatas ($ table ){
$ Query = "SELECT * FROM {$ table }";
$ This-> query ($ query );
Return $ this-> fetchAll ();
}
Protected function fetch (){
If (is_resource ($ this-> result )){
Return mysql_fetch_assoc ($ this-> result );
}
Return false;
}
Protected function fetchAll (){
If (is_resource ($ this-> result )){
$ Return = array ();
$ Row = NULL;
While ($ row = mysql_fetch_assoc ($ this-> result )){
$ Return [] = $ row;
}
Return $ return;
}
Return false;
}
}
?>