PHP backup database is SQL file and can download source code

Source: Internet
Author: User
Tags date insert join mysql query table name backup

This is a copy of all the tables in the specified database to a SQL file and can be downloaded. The source code from the DEDECMS program, the function is quite many, also very practical, but the quality of codes still need to improve

 
 
  1. <!? Php
  2. /****** BACKUP DATABASE Structure ******/
  3. /*
  4. Function name: Table2sql ()
  5. function function: Transform the structure of a table into SQL
  6. Function parameters: $table: Table name to be extracted
  7. Return value: Returns the result after extraction, SQL collection
  8. Function Author: heiyeluren
  9. */
  10. function Table2sql ($table)
  11. {
  12. Global $db;
  13. $tabledump = "DROP TABLE IF EXISTS $table; \ n";
  14. $createtable = $db--->query ("show CREATE TABLE $table");
  15. $create = $db->fetch_row ($createtable);
  16. $tabledump. = $create [1]. "; N\n ";
  17. return $tabledump;
  18. }
  19. /****** backup database structure and all data ******/
  20. /*
  21. Function name: Data2sql ()
  22. function function: Transform the structure and data of a table into SQL
  23. Function parameters: $table: Table name to be extracted
  24. Return value: Returns the result after extraction, SQL collection
  25. Function Author: heiyeluren
  26. */
  27. function Data2sql ($table)
  28. {
  29. Global $db;
  30. $tabledump = "DROP TABLE IF EXISTS $table; \ n";
  31. $createtable = $db->query ("Show CREATE TABLE $table");
  32. $create = $db->fetch_row ($createtable);
  33. $tabledump. = $create [1]. "; N\n ";
  34. $rows = $db->query ("SELECT * from $table");
  35. $numfields = $db->num_fields ($rows);
  36. $numrows = $db->num_rows ($rows);
  37. while ($row = $db->fetch_row ($rows))
  38. {
  39. $comma = "";
  40. $tabledump. = "INSERT into $table VALUES (";
  41. for ($i = 0; $i < $numfields; $i + +)
  42. {
  43. $tabledump. = $comma. "'". Mysql_escape_string ($row [$i]). "'";
  44. $comma = ",";
  45. }
  46. $tabledump. = "); \ n";
  47. }
  48. $tabledump. = "\ n";
  49. return $tabledump;
  50. }
  51. ?>
  52. <!--? php
  53. $host = "localhost"; Host Name
  54. $user = "root"; MySQL User name
  55. $password = "root"; Password
  56. $dbname = "DEDECMSV4"; Backed up databases
  57. Mysql_connect ($host, $user, $password);
  58. mysql_select_db ($dbname);
  59. $q 1=mysql_query ("Show Tables");
  60. while ($t =mysql_fetch_array ($q 1)) {
  61. $table = $t [0];
  62. $q 2=mysql_query ("Show create Table ' $table '");
  63. $sql =mysql_fetch_array ($q 2);
  64. $mysql. = $sql [' Create Table ']. " r\n\r\n "; #DDL
  65. $q 3=mysql_query ("select * from ' $table");
  66. while ($data =mysql_fetch_assoc ($q 3))
  67. {
  68. $keys =array_keys ($data);
  69. $keys =array_map (' addslashes ', $keys);
  70. $keys =join (', ', $keys);
  71. $keys = "'. $keys." `";
  72. $vals =array_values ($data);
  73. $vals =array_map (' addslashes ', $vals);
  74. $vals =join ("', '", $vals);
  75. $vals = "'" $vals. "'";
  76. $mysql. = "INSERT INTO ' $table ' ($keys) values ($vals); \ r \ n";
  77. }
  78. $mysql. = "\ r \ n";
  79. }
  80. $filename =date (' Ymd '). " _ ". $dbname.". SQL "; File name is the date of the day
  81. $fp = fopen ($filename, ' w ');
  82. Fputs ($fp, $mysql);
  83. Fclose ($FP);
  84. echo "Data backup succeeded, generate backup file". $filename;
  85. ?>




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.