Php simple backup and restoration of MySql, php restoration of mysql

Source: Internet
Author: User
Tags mysql backup

Php simple backup and restoration of MySql, php restoration of mysql

This example describes how to back up and restore MySql in php. 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

<! -- Author: Jelly qq: 52091199 blog: http://wyg517.blog.163.com --> <meta http-equiv = "Content-Type" content = "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 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; $ s Qls = 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, A total of $ total commands are processed. $ cg commands are successful, and $ sb commands fail. "; // the error message if ($ sb> 0) {echo" 

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.