My web application allow to get backups and set restore sql file.when restore script drop all the tables but some tables don't drop because there are foreign keys.so i added some code above of the restore.php file that will successfully execute in localhost but server it is not working.server not execute the SET FOREIGN_KEY_CHECKS = 0.
my local machine php version :5.38 Server machine php version :5.46
Thank you !!
here is the restore.php file
$remove_foreign_key = 'SET FOREIGN_KEY_CHECKS = 0';
$result = mysqli_query( $conn,$remove_foreign_key );
if( $result ) {
}
else{
echo "Error
";
}
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) . "
";
}
$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;
}
?>