This article provides a mysql2xml conversion class, you can quickly convert XML to MySQL and vice versa can also convert MySQL to XML Oh, look at the code below.
mysql2xml.php class file: Used to back up MySQL data!!
| The code is as follows |
Copy 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. = "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;
}
}
?> |
Call Method:
| The code is as follows |
Copy Code |
$xml = new Mysql2xml (' host ' = ' localhost ', ' username ' = ' root ', ' password ' + ' ', ' database ' = ' mysql ') );
$xml->settables (Array (' wp_term_relationships ', ' wp_terms '));//Set up a table for backup
$xml->setsavefolder (' datas/');//The folder where the backup files are saved
$xml->toxml ();//Backup started
?> |
http://www.bkjia.com/PHPjc/632929.html www.bkjia.com true http://www.bkjia.com/PHPjc/632929.html techarticle This article provides a mysql2xml conversion class, you can quickly convert XML to MySQL and vice versa can also convert MySQL to XML Oh, look at the code below. mysql2xml.php class file: For backup ...