This article provides a mysql2xml conversion class, which can quickly convert xml into mysql, and vice versa. Let's look at the code below.
Mysql2xml. php file: used to back up MySQL Data !!
| The Code is as follows: |
Copy code |
|
<? Php
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 = "<? Xml version = "1.0" encoding = "UTF-8"?> N <datas> 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 <fields name =" {$ element ['field']} "type =" {$ element ['type']} "null =" {$ element ['null ']} "key =" {$ element ['key']} "default =" {$ element ['default']} "extra =" {$ element ['extra' ]} "> n ";
Foreach ($ cdata [$ element ['field'] as $ value ){
$ Xml. = "tt <data >{$ value} </data> n ";
}
$ Xml. = "t </fields> n ";
}
$ Xml. = '</datas> ';
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;
}
}
?> |
Call method:
| The Code is as follows: |
Copy code |
<? Php
$ 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
?> |