When using PHP to develop Web applications, data may often need to be constantly converted between XML and MySQL. This article will introduce two functions to achieve conversion between XML and MySQL. Mysql2xml. php file: used to back up MySQL data!
PHP code
- 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. = "\ t{$ 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;
- }
- }
- ?>
Call method:
PHP code
- $ 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
- ?>