php xml2mysql_php mysql与xml数据相互转换程序

很多时间我们要把数据保存到其它地方,如保存到xml或又想把xml数据保存到mysql数据库,下面我们就提供了一款php mysql与xml数据相互转换程序哦。

先来看看调用方法

$xml = new MySQL2XML(array('host'=>'localhost', 'username'=>'root', 'password'=>'', 'database'=>'mysql'));

$xml->setTables(array('wp_term_relationships','wp_terms'));//设置备份的表

$xml->setSaveFolder('datas/');//保存备份文件的文件夹

$xml->toXML();//备份开始

?>

下面是这个互相转换程序的类型文件

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

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值