91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用php怎么對xml與mysql數據進行相互轉換

發布時間:2021-01-16 11:25:24 來源:億速云 閱讀:148 作者:Leah 欄目:開發技術

本篇文章為大家展示了利用php怎么對xml與mysql數據進行相互轉換,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

具體分析如下:

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

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

復制代碼 代碼如下:

<?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<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;
        }
}
?>


調用方法,代碼如下:

復制代碼 代碼如下:

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

上述內容就是利用php怎么對xml與mysql數據進行相互轉換,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

榆社县| 龙里县| 花莲县| 蚌埠市| 绩溪县| 乐亭县| 图木舒克市| 宜昌市| 忻州市| 灵川县| 诸城市| 西城区| 郎溪县| 丹凤县| 吴桥县| 沁阳市| 绥江县| 台南县| 隆化县| 石城县| 邓州市| 武威市| 中卫市| 金秀| 静宁县| 赫章县| 甘肃省| 清镇市| 东方市| 淄博市| 庐江县| 西盟| 孝昌县| 龙江县| 水富县| 湖南省| 张北县| 江北区| 平泉县| 商水县| 滦南县|