Use PHP to implement mutual conversion between XML and MySQL

Source: Internet
Author: User
When using PHP to develop Web applications, data may often need to be constantly converted between XML and MySQL. This article will introduce two functions to achieve conversion between XML and MySQL. Mysql2xml. php file: used to back up MySQL data!

PHP code

  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 =" \ 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{$ 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. ?>

Call method:

PHP code

  1. $ Xml = new MySQL2XML (array ('host' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'mysql '));
  2. $ Xml-> setTables (array ('WP _ term_relationships ', 'WP _ terms'); // you can specify a backup table.
  3. $ Xml-> setSaveFolder ('datas/'); // Save the folder of the backup file
  4. $ Xml-> toXML (); // backup start
  5. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.