问题
I have backup successfully but now i need to restore that data in localhost j my backup drive is C:\wamp\www\my Pawning Project\backup_restore this is my restore code.
<?php
include '../Connection/connect.php';
$restore_data = $_GET['restore_data'];//file time
$base="http://localhost/my%20Pawning%20Project/backup_restore/";
$query = "select * from backups where time='$restore_data'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$file_path =$row['file_name'];
}
$sql=file_get_contents($base.$file_path);
mysql_query($sql);
if(mysql_query($sql))
{
/*Success*/
echo "Successfully restored";
}
else
{
/*Fail*/
echo "Error: Fail to Restore";
}
?>
回答1:
You can use exec (or shell_exec) function in php :
exec("mysql -hHost -uUsername -pPassword database < ~/backupdir/backupFile.sql")
回答2:
You can't execute multiple statements with php mysql_*
queries. Therefore you should first get fully qualified path of the file you want to use and execute a SQL statement:
$path = $base . $file_path;
$query = "SOURCE $path"
mysql_query($query);
回答3:
If you were to use Mysqli (recommended over mysql_*) you could use the following
$sql = file_get_contents($file_location);
if($conn->multi_query($sql)){
while($conn->more_results() and $conn->next_result()){
;
}
};
回答4:
You're running the restore query twice
mysql_query($sql);
if(mysql_query($sql))
Remove the 1st and just do:
if(mysql_query($sql))
If your backup file contains many queries, this won't work. You'll need to do one of the following:
- Execute
mysql
directly and feed it the backup file (see Pierre's answer) - Switch from
mysql_
toMySQLi
functions and usemysqli::multi_query
(docs) - Trnsform the file contents into an array of queries with
explode(';',$sql)
then execute the queries on at a time by looping through the aray (will be slow)
来源:https://stackoverflow.com/questions/38484666/how-to-restore-backup-mysql-file-in-php