Here's the code:
First, back up the database and download to the local "db_backup.php"
Copy Code code as follows:
<?php
Set up SQL file Save file name
$filename =date ("Y-m-d_h-i-s"). " -". $cfg _dbname.". SQL ";
The file name that you saved
Header ("Content-disposition:filename=". $filename);
Header ("Content-type:application/octetstream");
Header ("Pragma:no-cache");
Header ("expires:0");
Gets the current paging file path, and the SQL file is exported to this folder
$tmpFile = (dirname (__file__)). " \ ". $filename;
Exporting a database with the mysqldump command
EXEC ("mysqldump-u$cfg_dbuser-p$cfg_dbpwd--default-character-set=utf8 $cfg _dbname >". $tmpFile);
$file = fopen ($tmpFile, "R"); Open File
Echo fread ($file, FileSize ($tmpFile));
Fclose ($file);
Exit
?>
Second, restore the database "db_restore.php"
Copy Code code as follows:
<form id= "Form1" Name= "Form1" method= "Post" action= "" >
"Database SQL file": <input id= "Sqlfile" name= "Sqlfile" type= "file"/>
<input id= "Submit" name= "submit" type= "submit" value= "Restore"/>
</form>
<?php
My database information is stored in the config.php file, so load this file, if you are not stored in the file, note this line can be;
Require_once (DirName (__file__). /.. /.. /include/config.php '));
if (Isset ($_post[' sqlfile '))
{
$file _name = $_post[' Sqlfile ']; The SQL file name to import
$dbhost = $cfg _dbhost; Database host Name
$dbuser = $cfg _dbuser; Database user Name
$dbpass = $cfg _dbpwd; Database Password
$dbname = $cfg _dbname; Database name
Set_time_limit (0); The timeout is set to 0, which indicates that it has been executed. When PHP is invalid in safe Mode mode, this may cause the import timeout, which requires a segmented import
$fp = @fopen ($file _name, "R") or Die ("Cannot open SQL file $file _name");/Open File
Mysql_connect ($dbhost, $dbuser, $dbpass) or Die ("Cannot connect to the database $dbhost");/Connection Database
mysql_select_db ($dbname) or Die ("Cannot open database $dbname");/Open Database
echo "<p> is emptying the database, please wait for ....<br>";
$result = mysql_query ("Show Tables");
while ($currow =mysql_fetch_array ($result))
{
mysql_query ("Drop TABLE IF EXISTS $currow [0]");
echo "clears the datasheet". $currow [0]. " Success <br> ";
}
echo "<br> congratulate you on cleaning MySQL success <br>";
echo "Performing Import Database Operations <br>";
MySQL command to import Database
EXEC ("Mysql-u$cfg_dbuser-p$cfg_dbpwd $cfg _dbname <". $file _name);
echo <br> Import complete! ";
Mysql_close ();
}
?>