Php simple backup and restoration of MySql _ php tips-php Tutorial

Source: Internet
Author: User
Tags mysql backup
This article mainly introduces php simple backup and restoration methods for MySql, involving php connection, query, and file operations related skills for mysql databases, for more information about how to back up and restore MySql in php, see the following example. We will share this with you for your reference. The details are as follows:

I. 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, and name are all passed from the page if (! Mysql_connect ($ host, $ user, $ password) // connection to mysql database {echo 'database connection failed. Please check and try again '; exit;} if (! Mysql_select_db ($ dbname) // check whether the database exists {echo 'database does not exist :'. $ dbname. ', check and try again'; exit;} mysql_query ("set names 'utf8'"); $ mysql = "set charset utf8; \ r \ n "; $ q1 = mysql_query ("show tables"); while ($ t = mysql_fetch_array ($ q1) {$ table = $ t [0]; $ q2 = mysql_query ("show create table '$ table'"); $ SQL = mysql_fetch_array ($ q2); $ mysql. = $ SQL ['create Table']. "; \ r \ n"; $ q3 = mysql_query ("select * from '$ table'"); while ($ data = mysql_fetch _ Assoc ($ q3) {$ 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, which is stored in the outermost layer of the project by default $ fp = fopen ($ filename, 'w'); fputs ($ fp, $ mysql ); Fclose ($ fp); echo "data backup succeeded";?>

II. restoration

 
 <? Php $ filename = "test20101216923. SQL"; $ host = "localhost"; // host name $ user = "root"; // MYSQL username $ password = "123456 "; // password $ dbname = "test"; // specify the name of the database to be restored. if the name does not exist, you must create the database first. modify the database name mysql_connect ($ host, $ user, $ password); mysql_select_db ($ dbname); $ mysql_file = "data /". $ filename; // specify the path of the MySQL backup file to be restored. modify this path restore ($ mysql_file) by yourself; // execute the MySQL recovery 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 "; \ r \ n "condition $ total = count ($ a)-1; mysql_query (" set names 'utf8' "); for ($ I = 0; $ I <$ total; $ I ++) {mysql_query ("set names 'utf8'"); // execute the command if (mysql_query ($ a [$ I]) {$ cg + = 1;} else {$ sb + = 1; $ sb_command [$ sb] = $ a [$ I] ;}} echo "operation complete, $ total commands are processed. $ cg commands are successful. $ sb commands fail to be processed. "; // Display error message if ($ sb> 0) {echo"

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

". $ Ii." command (content as follows ):
". $ Sb_command [$ ii]."


";}}// -------------------------------------------------------------} Else {echo" the MySQL backup file does not exist. check whether the file path is correct! ";}}?>

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.