This article is an example of how to implement XML and MySQL data conversion in PHP. Share to everyone for your reference. The specific analysis is as follows:
This provides a mysql2xml conversion class that can quickly convert XML to MySQL while converting MySQL to XML, looking at the code below.
mysql2xml.php class files, used to back up MySQL data, the code is as follows:
Copy Code code as follows:
<?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 the method, the code is as follows:
Copy Code code as follows:
<?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/');//folder where backup files are saved
$xml->toxml ()//start of Backup
?>
I hope this article will help you with your PHP program design.