php中實現xml與mysql資料相互轉碼_PHP教程

來源:互聯網
上載者:User
本文章提供了一個mysql2xml轉換類,可以快速的把xml轉換成mysql同時反之也可以把mysql轉換成xml哦,下面看代碼。

mysql2xml.php類檔案:用於備份MySQL資料的!!

代碼如下 複製代碼

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 = "nn";
$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 .= "tn";
foreach($cdata[$element['Field']] as $value) {
$xml .= "tt{$value}n";
}
$xml .= "tn";
}
$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;
}
}
?>


調用方法:

代碼如下 複製代碼
$xml = new MySQL2XML(array('host'=>'localhost', 'username'=>'root', 'password'=>'', 'database'=>'mysql'));
$xml->setTables(array('wp_term_relationships','wp_terms'));//設定備份的表
$xml->setSaveFolder('datas/');//儲存備份檔案的檔案夾
$xml->toXML();//備份開始
?>

http://www.bkjia.com/PHPjc/632929.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632929.htmlTechArticle本文章提供了一個mysql2xml轉換類,可以快速的把xml轉換成mysql同時反之也可以把mysql轉換成xml哦,下面看代碼。 mysql2xml.php類檔案:用於備份...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.