php中實(shí)現(xiàn)xml與mysql數(shù)據(jù)相互轉(zhuǎn)換的方法
本文實(shí)例講述了php中實(shí)現(xiàn)xml與mysql數(shù)據(jù)相互轉(zhuǎn)換的方法。分享給大家供大家參考。具體分析如下:
這里提供了一個(gè)mysql2xml轉(zhuǎn)換類,可以快速的把xml轉(zhuǎn)換成mysql同時(shí)反之也可以把mysql轉(zhuǎn)換成xml,下面看代碼.
mysql2xml.php類文件,用于備份MySQL數(shù)據(jù)的,代碼如下:
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;
}
}
?>
調(diào)用方法,代碼如下:
$xml = new MySQL2XML(array('host'=>'localhost', 'username'=>'root', 'password'=>'', 'database'=>'mysql'));
$xml->setTables(array('wp_term_relationships','wp_terms'));//設(shè)置備份的表
$xml->setSaveFolder('datas/');//保存?zhèn)浞菸募奈募A
$xml->toXML();//備份開(kāi)始
?>
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- PHP實(shí)現(xiàn)動(dòng)態(tài)添加X(jué)ML中數(shù)據(jù)的方法
- PHP解析xml格式數(shù)據(jù)工具類示例
- PHP操作XML作為數(shù)據(jù)庫(kù)的類
- PHP中使用xmlreader讀取xml數(shù)據(jù)示例
- PHP生成和獲取XML格式數(shù)據(jù)的方法
- php操作XML、讀取數(shù)據(jù)和寫(xiě)入數(shù)據(jù)的實(shí)現(xiàn)代碼
- php處理復(fù)雜xml數(shù)據(jù)示例
- PHP XML備份Mysql數(shù)據(jù)庫(kù)
- php操作xml并將其插入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
- PHP XML數(shù)據(jù)解析代碼
- PHP實(shí)現(xiàn)動(dòng)態(tài)刪除XML數(shù)據(jù)的方法示例
相關(guān)文章
php中CI操作多個(gè)數(shù)據(jù)庫(kù)的代碼
最近有個(gè)需求,做一個(gè)獨(dú)立后臺(tái)出來(lái),這個(gè)獨(dú)立后臺(tái)有單獨(dú)的庫(kù),單獨(dú)的url,單獨(dú)…。但是,這里邊只是一些基礎(chǔ)數(shù)據(jù),而使用者需要通過(guò)該庫(kù)查看另一個(gè)庫(kù)里的統(tǒng)計(jì)數(shù)據(jù)。這就帶來(lái)一個(gè)問(wèn)題:我需要在控制器里邊可以同時(shí)操作兩個(gè)數(shù)據(jù)庫(kù),而且這兩個(gè)庫(kù)毫不相干2012-07-07php中字符集轉(zhuǎn)換iconv函數(shù)使用總結(jié)
這篇文章主要介紹了php中字符集轉(zhuǎn)換iconv函數(shù)使用總結(jié),本文同時(shí)介紹了mb_convert_encoding函數(shù),需要的朋友可以參考下2014-10-10php創(chuàng)建類并調(diào)用的實(shí)例方法
在本篇文章里小編給大家分享的是關(guān)于php如何創(chuàng)建類并調(diào)用的相關(guān)知識(shí)點(diǎn),有需要的朋友們可以學(xué)習(xí)下。2019-09-09PHP+Mysql+jQuery實(shí)現(xiàn)動(dòng)態(tài)展示信息
在本文中,我將介紹如何在頁(yè)面上實(shí)現(xiàn)動(dòng)態(tài)展示用戶發(fā)表的信息,將用戶發(fā)表的信息逐條播放展示。該效果可以在展示系統(tǒng)動(dòng)態(tài)、商品評(píng)論等場(chǎng)景應(yīng)用2011-10-10php socket通信(tcp/udp)實(shí)例分析
這篇文章主要介紹了php socket通信(tcp/udp)方法,結(jié)合實(shí)例形式分析了基于socket實(shí)現(xiàn)tcp與udp通信的相關(guān)技巧,需要的朋友可以參考下2016-02-02PHP在終端中實(shí)現(xiàn)進(jìn)度條來(lái)處理數(shù)據(jù)的示例詳解
在PHP中,有時(shí)候你需要在終端中處理大量數(shù)據(jù)或執(zhí)行長(zhǎng)時(shí)間運(yùn)行的任務(wù),同時(shí)希望能夠?qū)崟r(shí)跟蹤任務(wù)的進(jìn)度,在 PHP 中,有時(shí)候你需要在終端中處理大量數(shù)據(jù)或執(zhí)行長(zhǎng)時(shí)間運(yùn)行的任務(wù),同時(shí)希望能夠?qū)崟r(shí)跟蹤任務(wù)的進(jìn)度,需要的朋友可以參考下2023-10-10