我的Web应用程序允许获取备份并设置还原sql文件。当还原脚本删除所有表时,但是由于有外键,某些表没有删除。所以我在restore.php文件上方添加了一些代码在localhost上成功执行,但是服务器不起作用。服务器未执行 SET FOREIGN_KEY_CHECKS = 0 。
我的本地计算机php版本:5.38 服务器计算机php版本:5.46
谢谢!!
这是restore.php文件
$remove_foreign_key = 'SET FOREIGN_KEY_CHECKS = 0';
$result = mysqli_query( $conn,$remove_foreign_key );
if( $result ) {
}
else{
echo "Error\n";
}
if (! empty($_FILES)) {
// Validating SQL file type by extensions
if (! in_array(strtolower(pathinfo($_FILES["backup_file"]["name"], PATHINFO_EXTENSION)), array(
"sql"
))) {
$response = array(
"type" => "error",
"message" => "Invalid File Type"
);
} else {
if (is_uploaded_file($_FILES["backup_file"]["tmp_name"])) {
move_uploaded_file($_FILES["backup_file"]["tmp_name"], $_FILES["backup_file"]["name"]);
$response = restoreMysqlDB($_FILES["backup_file"]["name"], $conn);
}
}
}
function restoreMysqlDB($filePath, $conn)
{
$sql = '';
$error = '';
if (file_exists($filePath)) {
$lines = file($filePath);
foreach ($lines as $line) {
// Ignoring comments from the SQL script
if (substr($line, 0, 2) == '--' || $line == '') {
continue;
}
$sql .= $line;
if (substr(trim($line), - 1, 1) == ';') {
$result = mysqli_query($conn, $sql);
if (! $result) {
$error .= mysqli_error($conn) . "\n";
}
$sql = '';
}
} // end foreach
if ($error) {
$response = array(
"type" => "error",
"message" => $error
);
} else {
$response = array(
"type" => "success",
"message" => "Database Restore Completed Successfully."
);
}
unlink($filePath);
} // end if file exists
return $response;
}
?>