How to convert XML to MySQL data in PHP, xmlmysql_php tutorial

Source: Internet
Author: User
Tags cdata

PHP in the implementation of XML and MySQL data conversion between the method, Xmlmysql


In this paper, we explain how to convert XML to MySQL data in PHP. Share to everyone for your reference. The specific analysis is as follows:

Here is a mysql2xml conversion class that can quickly convert the XML to MySQL and vice versa can also convert MySQL to XML, see the code below.

Mysql2xml.php class file for backing up MySQL data, the code is as follows:
Copy CodeThe code is 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 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 the method with the following code:
Copy CodeThe code is as follows: <?php
$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
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/932476.html www.bkjia.com true http://www.bkjia.com/PHPjc/932476.html techarticle PHP in the implementation of XML and MySQL data conversion between the method, Xmlmysql The example of this article on the implementation of XML and MySQL data conversion between the method. Share to everyone for your reference. A specific analysis such as ...

  • Related Article

    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.