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 :-)