PHP _php Tutorial for Implementing XML and MySQL data conversion code

Source: Internet
Author: User
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 ...

  • 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.