欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

利用PHP实现 XML和MySQL的相互转换_PHP

程序员文章站 2024-01-18 20:29:10
...
mysql2xml.php类文件:用于备份MySQL数据的!

PHP代码

  1. class MySQL2XML {
  2. protected $conn;
  3. protected $result;
  4. protected $tables;
  5. protected $saveFolder = 'datas/';
  6. public function __construct($config = NULL) {
  7. if($config !== NULL && is_array($config)) {
  8. $this->connect($config);
  9. }
  10. }
  11. public function connect($config) {
  12. $this->conn = mysql_connect($config['host'], $config['username'], $config['password']);
  13. if($this->conn) {
  14. mysql_select_db($config['database']);
  15. return true;
  16. }
  17. return false;
  18. }
  19. public function setSaveFolder($folder) {
  20. if(is_dir($folder)) {
  21. $this->saveFolder = rtrim(str_replace("\\", "/", $folder),'/');
  22. return true;
  23. }
  24. return false;
  25. }
  26. public function setTables($tables) {
  27. if(is_array($tables)) {
  28. $this->tables = $tables;
  29. return true;
  30. }
  31. return false;
  32. }
  33. public function query($query) {
  34. if(!isset($query) || trim($query) == '') return false;
  35. $this->result = mysql_query($query);
  36. if($this->result) return true;
  37. return false;
  38. }
  39. public function toXML() {
  40. if(!isset($this->tables)) return false;
  41. foreach($this->tables as $table) {
  42. $file = $this->saveFolder.$table.'.xml';
  43. $fp = @fopen($file, 'w');
  44. if(!$fp) exit('Can not write file');
  45. fwrite($fp, $this->tableToXML($table));
  46. fclose($fp);
  47. unset($fp);
  48. }
  49. return true;
  50. }
  51. public function tableToXML($table) {
  52. header("content-type:text/xml;charset=utf-8");
  53. $xml = ""1.0\" encoding=\"utf-8\" ?>\n\n";
  54. $fields = $this->getFields($table);
  55. $datas = $this->getDatas($table);
  56. $cdata = array();
  57. foreach($datas as $data) {
  58. foreach($data as $key => $value)
  59. $cdata[$key][] = $value;
  60. }
  61. foreach($fields as $element) {
  62. $xml .= "\t\n";
  63. foreach($cdata[$element['Field']] as $value) {
  64. $xml .= "\t\t{$value}\n";
  65. }
  66. $xml .= "\t\n";
  67. }
  68. $xml .= '';
  69. return $xml;
  70. }
  71. protected function getFields($table) {
  72. $query = "SHOW FIELDS FROM {$table}";
  73. $this->query($query);
  74. return $this->fetchAll();
  75. }
  76. protected function getDatas($table) {
  77. $query = "SELECT * FROM {$table}";
  78. $this->query($query);
  79. return $this->fetchAll();
  80. }
  81. protected function fetch() {
  82. if(is_resource($this->result)) {
  83. return mysql_fetch_assoc($this->result);
  84. }
  85. return false;
  86. }
  87. protected function fetchAll() {
  88. if(is_resource($this->result)) {
  89. $return = array();
  90. $row = NULL;
  91. while($row = mysql_fetch_assoc($this->result)) {
  92. $return[] = $row;
  93. }
  94. return $return;
  95. }
  96. return false;
  97. }
  98. }
  99. ?>

调用方法:

PHP代码

  1. $xml = new MySQL2XML(array('host'=>'localhost', 'username'=>'root', 'password'=>'', 'database'=>'mysql'));
  2. $xml->setTables(array('wp_term_relationships','wp_terms'));//设置备份的表
  3. $xml->setSaveFolder('datas/');//保存备份文件的文件夹
  4. $xml->toXML();//备份开始
  5. ?>