Use php to export a mysql database backup to an SQL example

Source: Internet
Author: User
Using php code to back up a database makes website management very convenient. we can back up the database directly in the background. key technologies: 1. first, you need to obtain the tables in the database. the Function mysql_list_tables ()... using php code to back up a database makes website management very convenient. we can back up the database directly in the background.

Key technologies:

1. First, you need to obtain the tables in the database, and use the mysql_list_tables () function. then, you can save all the obtained table names to an array.

2. the show create table name can obtain the table structure.

3. select * from table names to retrieve all records and concatenate them into insert into... statements in a loop.

The exported SQL statement is as follows:

 "; $ Info =" -- ---------------------------- rn "; $ info. = "-- date :". date ("Y-m-d H: I: s", time ()). "rn"; $ info. = "-- Power by Ruibo ( http://www.phprm.com/read/34.html ) Rn "; $ info. = "-- only used for testing and learning. this program is not suitable for processing large amounts of data rn"; $ info. = "-- -------------------------- rnrn"; file_put_contents ($ to_file_name, $ info, FILE_APPEND); // export the table structure of each table to the file foreach ($ tabList as $ val) {$ SQL = "show create table ". $ val; $ res = mysql_query ($ SQL, $ link); $ row = mysql_fetch_array ($ res); $ info = "-- -------------------------- rn"; $ info. = "-- Table structure "'. $ val. "'RN"; $ info. = "---------- -------------------- Rn "; $ info. = "drop table if exists "'. $ val. "'; rn"; $ sqlStr = $ info. $ row [1]. "; rnrn"; // append to the file file_put_contents ($ to_file_name, $ sqlStr, FILE_APPEND); // release the resource mysql_free_result ($ res );} // export the data of each table to the file foreach ($ tabList as $ val) {$ SQL = "select * from ". $ val; $ res = mysql_query ($ SQL, $ link); // if the table has no data, continue to the next table if (mysql_num_rows ($ res) <1) continue; // $ info = "-------------------------- ---- Rn "; $ info. = "-- Records "'. $ val. "'RN"; $ info. = "-- ---------------------------- rn"; file_put_contents ($ to_file_name, $ info, FILE_APPEND); // read data while ($ row = mysql_fetch_row ($ res )) {$ sqlStr = "insert "'. $ val. "'values ("; foreach ($ row as $ zd) {$ sqlStr. = "'". $ zd. "',";} // remove the last comma and space $ sqlStr = substr ($ sqlStr, 0, strlen ($ sqlStr)-2); $ sqlStr. = "); rn"; file_put_contents ($ to_file_name, $ s QlStr, FILE_APPEND);} // release the resource mysql_free_result ($ res); file_put_contents ($ to_file_name, "rn", FILE_APPEND);} echo "OK! ";

Address:

Reprinted at will, but please attach the article address :-)

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.