php 数据库在线备份代码
函数 sqldumptable($table, $fp=0) {
$tabledump = "如果存在 $table;n";
则删除表
$tabledump .= "创建表 $table (n";
$firstfield=1;
$fields = mysql_query("显示 $table 中的字段");
while ($field = mysql_fetch_array($fields)) {
if (!$firstfield) {
$tabledump .= ",n";
} 其他 {
$firstfield=0;
}
$tabledump .= " $field[Field] $field[Type]";
if (!empty($field["默认"])) {
$tabledump .= " DEFAULT '$field[Default]'";
}
if ($field['Null'] != "YES") {
$tabledump .= " NOT NULL";
}
if ($field['Extra'] != "") {
$tabledump .= " $field[Extra]";
}
}
mysql_free_result($fields);
$keys = mysql_query("显示$表中的键");
while ($key = mysql_fetch_array($keys)) {
$kname=$key['Key_name'];
if ($kname != "PRIMARY" and $key['Non_unique'] == 0) {
$kname="UNIQUE|$kname";
}
if(!is_array($index[$kname])) {
$index[$kname] = array();
}
$index[$kname][] = $key['Column_name'];
}
mysql_free_result($keys);
while(list($kname, $columns) = @each($index)) {
$tabledump .= ",n";
$colnames=implode($columns,",");
if ($kname == "PRIMARY") {
$tabledump .= " 主键 ($colnames)";
} 其他 {
if (substr($kname,0,6) == "唯一") {
$kname=substr($kname,7);
}
$tabledump .= " KEY $kname ($colnames)";
}
}
$tabledump .= "n);nn";
如果 ($fp) {
fwrite($fp,$tabledump);
} 其他 {
回显 $tabledump;
}
$rows = mysql_query("SELECT * FROM $table");
$numfields = mysql_num_fields($rows);
while ($row = mysql_fetch_array($rows)) {
$tabledump = "插入$table VALUES(";
$fieldcounter=-1;
$firstfield=1;
while ( $fieldcounter
if (!$firstfield) {
$tabledump.=", ";
} 其他 {
$firstfield=0;
}
if (!isset($row[$fieldcounter])) {
$tabledump .= "NULL";
} 其他 {
$tabledump .= "'".mysql_escape_string($row[$fieldcounter])."'";
}
}
$tabledump .= ");n";
如果 ($fp) {
fwrite($fp,$tabledump);
} 其他 {
回显 $tabledump;
}
}
mysql_free_result($rows);
}
@mysql_connect($servername,$dbusername,$dbpassword) or die("数据库连接失败");
@mysql_select_db($dbname) or die("选择数据库失败");
$table = array_flip($_POST['table']);
$result = mysql_query("显示表");
回声($结果)? NULL : "错误: ".mysql_error();
$filename = basename($_SERVER['HTTP_HOST']."_MySQL.sql");
header('内容类型:应用程序/未知');
header('内容处理:附件; filename='.$filename);
$mysqldata = '';
while ($currow = mysql_fetch_array($result)) {
if (isset($table[$currow[0]])) {
$mysqldata.= sqldumptable($currow[0]);
$mysqldata.= $mysqldata."rn";
}
}
mysql_close();
退出;