国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php中實現xml與mysql數據相互轉換代碼

2024-05-04 21:49:20
字體:
來源:轉載
供稿:網友

本文章提供了一個mysql2xml轉換類,可以快速的把xml轉換成mysql同時反之也可以把mysql轉換成xml,下面看代碼.

mysql2xml.php類文件,用于備份MySQL數據的,代碼如下:

  1. <?php 
  2. class MySQL2XML { 
  3.         protected $conn
  4.         protected $result
  5.         protected $tables
  6.         protected $saveFolder = 'datas/'
  7.          
  8.         public function __construct($config = NULL) { 
  9.                 if($config !== NULL && is_array($config)) { 
  10.                         $this->connect($config); 
  11.                 } 
  12.         } 
  13.          
  14.         public function connect($config) { 
  15.                 $this->conn = mysql_connect($config['host'], $config['username'], $config['password']); 
  16.                 if($this->conn) { 
  17.                         mysql_select_db($config['database']); 
  18.                         return true; 
  19.                 } 
  20.                 return false; 
  21.         } 
  22.          
  23.         public function setSaveFolder($folder) { 
  24.                 if(is_dir($folder)) { 
  25.                         $this->saveFolder = rtrim(str_replace("/", "/", $folder),'/'); 
  26.                         return true; 
  27.                 } 
  28.                 return false; 
  29.         } 
  30.          
  31.         public function setTables($tables) { 
  32.                 if(is_array($tables)) { 
  33.                         $this->tables = $tables
  34.                         return true; 
  35.                 } 
  36.                 return false; 
  37.         } 
  38.          
  39.         public function query($query) { 
  40.                 if(!isset($query) || trim($query) == ''return false; 
  41.                 $this->result = mysql_query($query); 
  42.                 if($this->result) return true; 
  43.                 return false; 
  44.         } 
  45.          
  46.         public function toXML() { 
  47.                 if(!isset($this->tables)) return false; 
  48.                 foreach($this->tables as $table) { 
  49.                         $file = $this->saveFolder.$table.'.xml'
  50.                         $fp = @fopen($file'w'); 
  51.                         if(!$fpexit('Can not write file'); 
  52.                         fwrite($fp$this->tableToXML($table)); 
  53.                         fclose($fp); 
  54.                         unset($fp); 
  55.                 } 
  56.                 return true; 
  57.         } 
  58.          
  59.         public function tableToXML($table) { 
  60.                 header("content-type:text/xml;charset=utf-8"); 
  61.                 $xml = "<?xml version="1.0" encoding="utf-8" ?>n<datas>n"
  62.                 $fields = $this->getFields($table); 
  63.                 $datas = $this->getDatas($table); 
  64.                 $cdata = array(); 
  65.                 foreach($datas as $data) { 
  66.                         foreach($data as $key => $value
  67.                                 $cdata[$key][] = $value
  68.                 } 
  69.                 foreach($fields as $element) { 
  70.                         $xml .= "t<fields name="{$element['Field']}" type="{$element['Type']}" null="{$element['Null']}" key="{$element['Key']}" default="{$element['Default']}" extra="{$element['Extra']}">n"
  71.                         foreach($cdata[$element['Field']] as $value) { 
  72.                                 $xml .= "tt<data>{$value}</data>n"
  73.                         } 
  74.                         $xml .= "t</fields>n"
  75.                 } 
  76.                 $xml .= '</datas>'
  77.                 return $xml
  78.         } 
  79.          
  80.         protected function getFields($table) { 
  81.                 $query = "SHOW FIELDS FROM {$table}"
  82.                 $this->query($query); 
  83.                 return $this->fetchAll(); 
  84.         }//開源代碼Vevb.com 
  85.          
  86.         protected function getDatas($table) { 
  87.                 $query = "SELECT * FROM {$table}"
  88.                 $this->query($query); 
  89.                 return $this->fetchAll(); 
  90.         } 
  91.          
  92.         protected function fetch() { 
  93.                 if(is_resource($this->result)) { 
  94.                         return mysql_fetch_assoc($this->result); 
  95.                 } 
  96.                 return false; 
  97.         } 
  98.          
  99.         protected function fetchAll() { 
  100.                 if(is_resource($this->result)) { 
  101.                         $return = array(); 
  102.                         $row = NULL; 
  103.                         while($row = mysql_fetch_assoc($this->result)) { 
  104.                                 $return[] = $row
  105.                         } 
  106.                         return $return
  107.                 } 
  108.                 return false; 
  109.         } 
  110. ?> 

調用方法,代碼如下:

  1. <?php 
  2. $xml = new MySQL2XML(array('host'=>'localhost''username'=>'root''password'=>'''database'=>'mysql')); 
  3. $xml->setTables(array('wp_term_relationships','wp_terms'));//設置備份的表 
  4. $xml->setSaveFolder('datas/');//保存備份文件的文件夾 
  5. $xml->toXML();//備份開始 
  6. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高雄县| 开江县| 孝感市| 仙游县| 双辽市| 福鼎市| 兴宁市| 扶沟县| 剑阁县| 甘谷县| 黄陵县| 卓资县| 阳东县| 岳西县| 杭锦旗| 阿拉善左旗| 金沙县| 新蔡县| 山东| 乐东| 永胜县| 综艺| 曲松县| 宜宾县| 科技| 莱阳市| 海南省| 万年县| 革吉县| 万宁市| 富宁县| 长宁区| 漳平市| 双城市| 齐齐哈尔市| 宣武区| 栖霞市| 灌云县| 德兴市| 周宁县| 乌兰浩特市|