php中实现xml与mysql数据相互转换的方法
2023-06-13 09:15:37 时间
本文实例讲述了php中实现xml与mysql数据相互转换的方法。分享给大家供大家参考。具体分析如下:
这里提供了一个mysql2xml转换类,可以快速的把xml转换成mysql同时反之也可以把mysql转换成xml,下面看代码.
mysql2xml.php类文件,用于备份MySQL数据的,代码如下:
classMySQL2XML{
protected$conn;
protected$result;
protected$tables;
protected$saveFolder="datas/";
publicfunction__construct($config=NULL){
if($config!==NULL&&is_array($config)){
$this->connect($config);
}
}
publicfunctionconnect($config){
$this->conn=mysql_connect($config["host"],$config["username"],$config["password"]);
if($this->conn){
mysql_select_db($config["database"]);
returntrue;
}
returnfalse;
}
publicfunctionsetSaveFolder($folder){
if(is_dir($folder)){
$this->saveFolder=rtrim(str_replace("\","/",$folder),"/");
returntrue;
}
returnfalse;
}
publicfunctionsetTables($tables){
if(is_array($tables)){
$this->tables=$tables;
returntrue;
}
returnfalse;
}
publicfunctionquery($query){
if(!isset($query)||trim($query)=="")returnfalse;
$this->result=mysql_query($query);
if($this->result)returntrue;
returnfalse;
}
publicfunctiontoXML(){
if(!isset($this->tables))returnfalse;
foreach($this->tablesas$table){
$file=$this->saveFolder.$table.".xml";
$fp=@fopen($file,"w");
if(!$fp)exit("Cannotwritefile");
fwrite($fp,$this->tableToXML($table));
fclose($fp);
unset($fp);
}
returntrue;
}
publicfunctiontableToXML($table){
header("content-type:text/xml;charset=utf-8");
$xml="<?xmlversion="1.0"encoding="utf-8"?>n<datas>n";
$fields=$this->getFields($table);
$datas=$this->getDatas($table);
$cdata=array();
foreach($datasas$data){
foreach($dataas$key=>$value)
$cdata[$key][]=$value;
}
foreach($fieldsas$element){
$xml.="t<fieldsname="{$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;
}
protectedfunctiongetFields($table){
$query="SHOWFIELDSFROM{$table}";
$this->query($query);
return$this->fetchAll();
}
protectedfunctiongetDatas($table){
$query="SELECT*FROM{$table}";
$this->query($query);
return$this->fetchAll();
}
protectedfunctionfetch(){
if(is_resource($this->result)){
returnmysql_fetch_assoc($this->result);
}
returnfalse;
}
protectedfunctionfetchAll(){
if(is_resource($this->result)){
$return=array();
$row=NULL;
while($row=mysql_fetch_assoc($this->result)){
$return[]=$row;
}
return$return;
}
returnfalse;
}
}
?>
调用方法,代码如下:
$xml=newMySQL2XML(array("host"=>"localhost","username"=>"root","password"=>"","database"=>"mysql"));
$xml->setTables(array("wp_term_relationships","wp_terms"));//设置备份的表
$xml->setSaveFolder("datas/");//保存备份文件的文件夹
$xml->toXML();//备份开始
?>
希望本文所述对大家的php程序设计有所帮助。
相关文章
- 实现MySQL远程数据库的备份与恢复(mysql备份远程数据库)
- MySQL实现数字转换为字符串(mysql数字转字符串)
- 如何在MySQL中使用枚举索引(mysql枚举索引)
- 基于PHP的MySQL访问方式(php访问mysql)
- MySQL日期转换函数:轻松解决时间转换困境(mysql日期转换函数)
- MySQL实现动态行列转换(mysql动态行列转换)
- Mysql:从字符串转换为时间(mysql字符转时间)
- MySQL时间戳转换为字符串的方法(mysql时间转换字符串)
- 编程开发之路:PHP 与 MySQL 结缘(php与mysql程序设计)
- 数据库快速学会:用PHP建立MySQL数据库(php建立mysql)
- PHP实现Object Oriented Stye封装MySQL类(php封装mysql类)
- MySQL日期转换为毫秒的方法(mysql日期转毫秒)
- MySQL命令行执行简介(mysql执行命令)
- 京东数据库迈向高性能:优化MySQL架构加速业务发展(mysql高性能京东)
- 修复MySQL文件损坏:如何进行数据库修复(mysql的数据库文件)
- PHP的MySQL编程示例(php的mysql代码)
- MySQL数据库的空间有多大?(mysql空间多大)
- MySQL和PHP:构建更先进的开发体系(mysql 和php)
- 快速掌握常用Mysql语句,让你的MySQL更强大(常用mysql语句)
- 使用MySQL学习韩语:让你学下汗语更容易!(mysql韩语)
- 查看MySQL版本:一步轻松搞定(看mysql版本)
- MySQL 57 版本安装指南(5.7版本mysql安装)
- 2008年重新启动MySQL数据库之路(2008重启mysql)
- MySQL如何删除表快速学习删除表语句(mysql中 删除表语句)
- MySQL语法中不包含哪些语句(mysql不包含的语句)
- MySQL中的自增选项无法选择(mysql不能选自增选项)
- MySQL安装要求不符合,如何解决(mysql不满足安装要求)
- PHP+Mysql日期时间如何转换(UNIX时间戳和格式化日期)