This article illustrates the simple way to backup and restore MySQL in PHP. Share to everyone for your reference, specific as follows:
First, Backup:
<?php header ("content-type:text/html;
Charset=utf-8 ");
Backup Database $host = "localhost"; $user = "root";//Database account $password = "123456";//Database Password $dbname = "Test";//Database name//The account, 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 there is the database {echo ' does not exist: '. $dbname. ', please check and try again ';
Exit
} mysql_query ("Set names ' UTF8 '");
$mysql = "Set CharSet utf8;\r\n";
$q 1=mysql_query ("Show Tables");
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 ";
Store path, default deposit to project outermost $fp = fopen ($filename, ' w ');
Fputs ($fp, $mysql);
Fclose ($FP);
echo "Data backup succeeded";?>
Two, restore
<!--author: Jelly qq:52091199 blog:http://wyg517.blog.163.com--> <meta http-equiv= "Content-type" text /html;
Charset=utf-8 "/> <?php $filename =" Test20101216923.sql "; $host = "localhost"; Host name $user = "root"; MySQL username $password = "123456"; Password $dbname = "Test";
Specify the name of the database you want to restore, if it does not exist, you must first create it, modify the database name mysql_connect ($host, $user, $password);
mysql_select_db ($dbname); $mysql _file= "data/". $filename; Specify the path to the MySQL backup file to restore, modify this path restore ($mysql _file);
Perform the 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);
$total =count ($a)-1 to be executed in the database according to the "; \ r \ n" condition;
mysql_query ("Set names ' UTF8 '");
for ($i =0; $i < $total; $i + +) {mysql_query ("Set names ' UTF8 '");
Executes the command if (mysql_query ($a [$i])) {$CG +=1;
else {$SB +=1;
$SB _command[$sb]= $a [$i];
"Echo" Operation completed, the total processing $total command, Success $CG, failure $SB Bar "; Show Error Letterif ($SB >0) {echo "
More about PHP Interested readers can view the site topics: "PHP based on PDO Operation Database Skills Summary", "PHP+MONGODB Database Operation Skills Encyclopedia", "PHP object-oriented Programming Program Introduction", "PHP string (String) Usage Summary", " Introduction to PHP+MYSQL Database operations and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.