我正在尝试将每个数据库备份到其自己的文件中。
我有下面的脚本,但是每次循环运行时,它都会将以前的表添加到其中,我哪里出错了?
// Get All Table Names From the Database
$tables = array();
$sql = "SHOW TABLES";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$tables[] = $row[0];
}
$sqlScript = "";
foreach ($tables as $table) {
// Prepare SQLscript for creating table structure
$query = "SHOW CREATE TABLE $table";
// echo"SHOW CREATE TABLE $table<br>";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_row($result);
$sqlScript .= "\n\n" . $row[1] . ";\n\n";
$query = "SELECT * FROM $table";
$result = mysqli_query($conn, $query);
$columnCount = mysqli_num_fields($result);
// Prepare SQLscript for dumping data for each table
for ($i = 0; $i < $columnCount; $i ++) {
while ($row = mysqli_fetch_row($result)) {
$sqlScript .= "INSERT INTO $table VALUES(";
for ($j = 0; $j < $columnCount; $j ++) {
$row[$j] = $row[$j];
if (isset($row[$j])) {
$sqlScript .= '"' . $row[$j] . '"';
} else {
$sqlScript .= '""';
}
if ($j < ($columnCount - 1)) {
$sqlScript .= ',';
}
}
$sqlScript .= ");\n";
}
}
$sqlScript .= "\n";
$myfile = fopen("backups/$datetime-$table.sql", "w") or die("Unable to open file!");
$txt = "$sqlScript";
fwrite($myfile, $txt);
fclose($myfile);
}
我正在尝试为数据库中的每个表实现一个文件...
移动
$sqlScript = "";
插入 foreach 循环,以便为每个表重置此变量的值。