PHP simple backup and restore MySQL method mysql website client mysql command mysql installation

Source: Internet
Author: User
Tags mysql backup
This article describes the PHP simple backup and restore MySQL method. Share to everyone for your reference, as follows:

First, Backup:

<?phpheader ("content-type:text/html; Charset=utf-8 ");//Backup Database $host=" localhost "; $user =" root ";//Database account $password=" 123456 ";//Database Password $dbname=" test ";//Database name/ /Here the account, password, name are transmitted from the page if (!mysql_connect ($host, $user, $password)//connection to the 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"); 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 project outermost $fp = fopen ($filename, ' W '); Fputs ($fp, $mysql); fclose ($FP); echo "Data backup succeeded";? >

Second, restore

 
 
  
 <?php$filename = "Test20101216923.sql"; $host = "localhost"; Host name $user= "root"; MySQL user name $password= "123456"; Password $dbname= "Test"; Specify the name of the database you want to restore here, do not exist, must be created first, please 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 that you want to restore, and then modify this path from restore ($mysql _file);  Executes 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);  According to the "; \ r \ n" condition to the database sub-stripe 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, successfully $CG, failed $SB"; Display error message if ($SB >0) {echo]

The failed command is as follows:
"; for ($ii =1; $ii <= $sb; $ii + +) {echo "

Section ". $ii." (The contents are as follows):
". $SB _command[$ii]."


"; }}//-----------------------------------------------------------}else{echo "MySQL backup file does not exist, please check the file path is correct!" "; } }? >

More interested in PHP related content readers can view this site topic: "PHP based on PDO operation database Tips Summary", "PHP+MONGODB Database operation Skills Daquan", "PHP Object-oriented Programming tutorial", "PHP String Usage Summary", " Php+mysql database Operation Tutorial "and" PHP common database Operation Skills Summary "

I hope this article is helpful to you in PHP programming.

The above describes the PHP simple backup and restore MySQL method, including the content of MySQL, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.