Backing up the database as a SQL file

Source: Internet
Author: User
This is a backup of all the tables in the specified database to a SQL file that can be downloaded.
  1. /****** backing up the database structure ******/
  2. /*
  3. Function name: Table2sql ()
  4. function function: Convert the structure of a table into SQL
  5. Function parameters: $table: The name of the table to extract
  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]. "; N\n ";
  16. return $tabledump;
  17. }
  18. /****** backing up the database structure and all data ******/
  19. /*
  20. Function name: Data2sql ()
  21. function function: Convert the structure and data of a table into SQL
  22. Function parameters: $table: The name of the table to extract
  23. Return value: Returns the extracted result, 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]. "; N\n ";
  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. ?>
Related Article

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.