mysql2xml.php class file: For backup 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:
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 '))//Set up tables for backup $xml->setsavefolder (' datas/') //The folder where the backup file is saved $xml->toxml ();//backup start |