Header ("content-type:text/html; Charset=utf-8 ");
Backing Up the database
$host = "localhost";
$user = "root";//Database account
$password = "root";//Database Password
$dbname = "Test";//Database name
The account number, password, and name are all transmitted from the page.
if (!mysql_connect ($host, $user, $password))//Connect MySQL Database
{
Echo ' Database connection failed, please check and try again ';
Exit
}
if (!mysql_select_db ($dbname))//whether the database exists
{
Echo ' does not exist database: '. $dbname. ', please check and try again ';
Exit
}
mysql_query ("Set names ' UTF8 '");
$mysql = "Set CharSet utf8;\r\n";
$q 1=mysql_query ("Show Tables");
Var_dump ($q 1);d ie;
while ($t =mysql_fetch_array ($q 1)) {
$table = $t [0];
$q 2=mysql_query ("Show create Table ' $table '");
$sql =mysql_fetch_array ($q 2);
$mysql. = $sql [' Create Table ']. "; r\n ";
$q 3=mysql_query ("select * from ' $table '");
while ($data =mysql_fetch_assoc ($q 3)) {
$keys =array_keys ($data);
$keys =array_map (' addslashes ', $keys);
$keys =join ("', '", $keys);
$keys = "'". $keys. " `";
$vals =array_values ($data);
$vals =array_map (' addslashes ', $vals);
$vals =join ("', '", $vals);
$vals = "'". $vals. "'";
$mysql. = "INSERT INTO ' $table ' ($keys) values ($vals); \ r \ n";
}
}
$filename = "./data/". $dbname. Date (' Ymjgi '). ". SQL "; Storage path, default to the outermost layer of the project
$fp = fopen ($filename, ' w ');
Fputs ($fp, $mysql);
Fclose ($FP);
echo "Data backup succeeded"
Restores:
$filename = "Test20170215841.sql";
$host = "localhost"; Host Name
$user = "root"; MySQL User name
$password = "root"; Password
$dbname = "Test"; Specify the name of the database you want to restore here, do not exist, you must first create it, and then modify the database name from
Mysql_connect ($host, $user, $password);
mysql_select_db ($dbname);
$mysql _file= "./data/". $filename; Specify the path to the MySQL backup file that you want to restore, self-Modify this path
Restore ($mysql _file); Execute MySQL RESTORE command
function Restore ($fname)
{
if (file_exists ($fname)) {
$sql _value= "";
$CG = 0;
$SB = 0;
$sqls =file ($fname);
foreach ($sqls as $sql)
{
$sql _value.= $sql;
}
$a =explode ("; \ r \ n", $sql _value); According to the "; \ r \ n" condition to the database sub-bar execution
$total =count ($a)-1;
mysql_query ("Set names ' UTF8 '");
for ($i =0; $i < $total; $i + +)
{
mysql_query ("Set names ' UTF8 '");
Execute command
if (mysql_query ($a [$i]))
{
$CG +=1;
}
Else
{
$SB +=1;
$SB _command[$sb]= $a [$i];
}
}
echo "Operation completed, the total processing $total command, the success of $CG, failure $sb Bar";
Display error message
if ($SB >0)
{
echo " for ($ii =1; $ii <= $sb; $ii + +)
{
echo "<p><b>". $ii. ":</b><br>" (contents below) ". $SB _command[$ii]." </p><br> ";
}
} //-----------------------------------------------------------
}else{
echo "MySQL backup file does not exist, please check the file path is correct!" ";
}
}
MySQL Backup and restore