how to restore backup mysql file in php [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-25 08:57:36

问题


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:

  1. Execute mysql directly and feed it the backup file (see Pierre's answer)
  2. Switch from mysql_ to MySQLi functions and use mysqli::multi_query (docs)
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!