Skip to content

Instantly share code, notes, and snippets.

@JingwenTian
Created October 24, 2014 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JingwenTian/aeda815cf1ae86872b04 to your computer and use it in GitHub Desktop.
Save JingwenTian/aeda815cf1ae86872b04 to your computer and use it in GitHub Desktop.
数据库备份功能 - 参考开源博客系统Emlog: https://github.com/emlog/emlog/blob/master/src/admin/data.php
<?php
/**
* 备份数据库结构和所有数据
*
* @param string $table 数据库表名
* @return string
*/
function dataBak($table){
$DB = MySql::getInstance();
$sql = "DROP TABLE IF EXISTS $table;\n";
$createtable = $DB->query("SHOW CREATE TABLE $table");
$create = $DB->fetch_row($createtable);
$sql .= $create[1].";\n\n";
$rows = $DB->query("SELECT * FROM $table");
$numfields = $DB->num_fields($rows);
$numrows = $DB->num_rows($rows);
while ($row = $DB->fetch_row($rows)){
$comma = "";
$sql .= "INSERT INTO $table VALUES(";
for ($i = 0; $i < $numfields; $i++){
$sql .= $comma."'".mysql_escape_string($row[$i])."'";
$comma = ",";
}
$sql .= ");\n";
}
$sql .= "\n";
return $sql;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment