Phpmysql and xml Data Conversion Program-PHP source code

Source: Internet
Author: User
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;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.