Created
October 24, 2014 16:55
-
-
Save JingwenTian/aeda815cf1ae86872b04 to your computer and use it in GitHub Desktop.
数据库备份功能 - 参考开源博客系统Emlog: https://github.com/emlog/emlog/blob/master/src/admin/data.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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