MySQL2xml.php类文件:用于备份MySQL数据的!
调用方法:
PHP代码
- <?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();
- ?>
还原数据的类:
xml2MySQL.php类文件:用来还原MySQL数据的
PHP代码
- <?php
- require_once 'MySQL2xml.php';
- class XML2MySQL extends MySQL2XML {
- private $XMLFiles = array();
- private $tableName = NULL;
- private $fields = array();
- private $datas = array();
-
- public function __construct($config = NULL) {
- parent::__construct($config);
- if(!function_exists('simplexml_load_file')) throw new Exception("Your server isn't suppost this class.");
- }
-
- public function setXMLFiles($file) {
- if(is_array($file)) {
- foreach($file as $f) {
- if(file_exists($f)) {
- $this->XMLFiles[] = $f;
- return true;
- } else return false;
- }
- } else {
- if(file_exists($file)) {
- $this->XMLFiles[] = $file;
- return true;
- } else return false;
- }
- }
-
- public function getXMLFromFolder($dir) {
- if(!is_dir($dir)) return false;
- $dir = rtRIM(str_replace("\\", "/", $dir), '/').'/';
- $dp = @opendir($dir);
- if(!$dp) throw new Exception("Can not open folder");
- while(($f = readdir($dp)) !== false) {
- if($f != '.' && $f != '..') {
- if(!$this->setXMLFiles($dir.$f)) throw new Exception("Error:Files are not xml file or files are not exists.");
- }
- }
- closedir($dp);
- return true;
- }
-
- public function toMySQL() {
- $buff = '';
- foreach($this->XMLFiles as $xml) {
- $this->getDataFromXML($xml);
- $drop = 'DROP TABLE IF EXISTS `'.$this->tableName.'`;';
- if($this->query($drop)) $buff .= 'Drop table <font color="red">['.$this->tableName.']</font> <font color="green">success</font>'."<br/>\n";
- else $buff .= 'Drop table <font color="red">['.$this->tableName.']</font> <font color="red">fail</font>'."<br/>\n";
- $pk = NULL;
- $uk = NULL;
- $sql = 'CREATE TABLE `'.$this->tableName."`(\n";
- foreach($this->fields as $field) {
- $sql .= '`'.$field['name'].'`'.' '.$field['type'].' '.($field['null'] == 'NO' ? 'NOT NULL ':'').(tRIM($field['extra']) != '' ? strtoupper($field['extra']).' ' : '').(tRIM($field['default']) != '' ? 'DEFAULT '."'".$field['default']."'" : '').','."\n";
- if($field['key'] == 'PRI') $pk = $pk.(strpos($pk, 'PRIMARY KEY') !== FALSE ? '`'.$field['name'].'`,' : 'PRIMARY KEY (`'.$field['name'].'`,');
- if($field['key'] == 'UNI') $uk = $uk.(strpos($uk, 'UNIQUE KEY') !== FALSE ? '`'.$field['name'].'`,' : "UNIQUE KEY `".$field['name']."` (`".$field['name']."`,");
- $fields[$this->tableName][] = $field['name'];
- }
- if($pk !== NULL) {
- $pk = rtRIM($pk, ",")."),\n";
- $sql .= $pk;
- }
- if($uk !== NULL) {
- $uk = rtRIM($uk, ",")."),\n";
- $sql .= $uk;
- }
- $sql = rtRIM($sql, ",\n");
- $sql .= ');';
- if($this->query($sql)) $buff .= 'Create table <font color="red">['.$this->tableName.']</font> <font color="green">success</font>'."<br/>\n";
- else $buff .= 'Create table <font color="red">['.$this->tableName.']</font> <font color="red">fail</font>'."<br/>\n";
- unset($sql);
-
- $datas = 'INSERT INTO `'.$this->tableName.'` (';
- foreach($fields as $table_name => $f) {
- foreach($f as $element) {
- $datas .= '`'.$element.'`,';
- }
- }
- $this->datas = $this->r2l($this->datas);
- $datas = rtRIM($datas, ',').') VALUES ';
- foreach($this->datas as $data) {
- $datas .= "(";
- foreach($data as $d) {
- $datas .= "'".$d."',";
- }
- $datas = rtRIM($datas, ',');
- $datas .= '),';
- }
- $datas = rtRIM($datas, ',').';';
- if($this->query($datas)) $buff .= 'Insert data in table <font color="red">['.$this->tableName.']</font> <font color="green">success</font>'."<br/><br/>\n";
- else $buff .= 'Insert data in table <font color="red">['.$this->tableName.']</font> <font color="red">fail</font>'."<br/><br/>\n";
- unset($fields);
- }
- return $buff;
- }
-
- private function r2l($array) {
- $temp = array();
- for($i = 0; $i < count($array); $i++) {
- for($j = 0; $j < count($array[0]); $j++) {
- $temp[$j][$i] = $array[$i][$j];
- }
- }
- return $temp;
- }
-
- private function getDataFromXML($xml) {
-
- $this->tableName = substr(basename($xml), 0, strlen(basename($xml)) - 4);
- $simplexml = simplexml_load_file($xml);
- $fields = array();
- $index = 0;
- foreach($simplexml->children() as $e) {
- $fields[$index]['name'] = (string)$e->attributes()->name;
- $fields[$index]['type'] = (string)$e->attributes()->type;
- $fields[$index]['null'] = (string)$e->attributes()->null;
- $fields[$index]['key'] = (string)$e->attributes()->key;
- $fields[$index]['default'] = (string)$e->attributes()->default;
- $fields[$index]['extra'] = (string)$e->attributes()->extra;
- $index++;
- }
- $this->fields = $fields;
-
- $datas = array();
- $index = 0;
- foreach($simplexml->children() as $e) {
- foreach($e->children() as $d) {
- $datas[$index][] = (string)$d;
- }
- $index++;
- }
- $this->datas = $datas;
- }
- }
- ?>
调用方法:
PHP代码
- <?php
- $s = new XML2MySQL(array('host'=>'localhost', 'username'=>'root', 'password'=>'', 'database'=>'MySQL'));
- $s->getXMLFromFolder('datas/');
- echo $s->toMySQL();
- ?>
类别:HTML,ASP,JSP,PHP 来源:本站原创 作者:hpping 日期:2010-02-20 14:21
上一条:PHP实现域名whois查询 数据源万网、新网
下一条:HTML5定稿了,为什么原生App世界将被颠覆