Home > Backend Development > PHP Tutorial > Backup database as SQL file

Backup database as SQL file

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 09:11:16
Original
1579 people have browsed it
This is a backup of all tables in a specified database into a SQL file, which can be downloaded.
  1. /****** Back up database structure ******/
  2. /*
  3. Function name: table2sql()
  4. Function function: Convert the table structure into SQL
  5. Function parameters: $table: To be performed Extracted table name
  6. Return value: Returns the extracted result, SQL collection
  7. Function author: heiyeluren
  8. */
  9. function table2sql($table)
  10. {
  11. global $db;
  12. $tabledump = "DROP TABLE IF EXISTS $table ;n";
  13. $createtable = $db--->query("SHOW CREATE TABLE $table");
  14. $create = $db->fetch_row($createtable);
  15. $tabledump .= $create[1 ].";nn";
  16. return $tabledump;
  17. }
  18. /****** Back up the database structure and all data ******/
  19. /*
  20. Function name: data2sql()
  21. Function: Convert the table structure and data into SQL
  22. Function Parameters: $table: The name of the table to be extracted
  23. Return value: Return the extracted results, SQL collection
  24. Function author: heiyeluren
  25. */
  26. function data2sql($table)
  27. {
  28. global $db;
  29. $tabledump = " DROP TABLE IF EXISTS $table;n";
  30. $createtable = $db->query("SHOW CREATE TABLE $table");
  31. $create = $db->fetch_row($createtable);
  32. $tabledump .= $create[1].";nn";
  33. $rows = $db->query("SELECT * FROM $table");
  34. $numfields = $db->num_fields($rows);
  35. $numrows = $db->num_rows($rows);
  36. while ($row = $db->fetch_row($rows))
  37. {
  38. $comma = "";
  39. $tabledump .= "INSERT INTO $table VALUES( ";
  40. for($i = 0; $i < $numfields; $i++)
  41. {
  42. $tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
  43. $ comma = ",";
  44. }
  45. $tabledump .= ");n";
  46. }
  47. $tabledump .= "n";
  48. return $tabledump;
  49. }
  50. ?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template