利用PHP实现 XML和MySQL的相互转换_PHP

WBOY
풀어 주다: 2016-06-01 12:20:48
원래의
893명이 탐색했습니다.

mysql2xml.php类文件:用于备份MySQL数据的!

PHP代码

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

调用方法:

PHP代码

  1. $xml = new MySQL2XML(array('host'=>'localhost''username'=>'root''password'=>'''database'=>'mysql'));   
  2. $xml->setTables(array('wp_term_relationships','wp_terms'));//设置备份的表   
  3. $xml->setSaveFolder('datas/');//保存备份文件的文件夹   
  4. $xml->toXML();//备份开始   
  5. ?>  

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!