Heim  >  Artikel  >  Backend-Entwicklung  >  备份数据库为SQL文件

备份数据库为SQL文件

WBOY
WBOYOriginal
2016-07-25 09:11:161468Durchsuche
这是一个将指定数据库里的所有表备份为一个SQL文件,可下载。
  1. /****** 备份数据库结构 ******/
  2. /*
  3. 函数名称:table2sql()
  4. 函数功能:把表的结构转换成为SQL
  5. 函数参数:$table: 要进行提取的表名
  6. 返 回 值:返回提取后的结果,SQL集合
  7. 函数作者: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. /****** 备份数据库结构和所有数据 ******/
  19. /*
  20. 函数名称:data2sql()
  21. 函数功能:把表的结构和数据转换成为SQL
  22. 函数参数:$table: 要进行提取的表名
  23. 返 回 值:返回提取后的结果,SQL集合
  24. 函数作者: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 {
  41. $tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
  42. $comma = ",";
  43. }
  44. $tabledump .= ");\n";
  45. }
  46. $tabledump .= "\n";
  47. return $tabledump;
  48. }
  49. ?>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:COOKIE加密函数 Nächster Artikel:转换ubb代码的一个函数