This is a backup of all the tables in the specified database as an SQL file that can be downloaded. This source code from dedecms procedures, functions quite a lot, but also very useful, but the quality of the code needs to be improved
<!? php / ****** backup database structure ****** / / * function name: table2sql () function: the structure of the table converted to SQL function parameters: $ table: to extract the table return value: returns the result of the extraction, SQL set of functions: heiyeluren * / function table2sql ($ table) {global $ db; $ tabledump = "DROP TABLE IF EXISTS $ table; n"; $ createtable = $ db-- -> query ("SHOW CREATE TABLE $ table"); $ create = $ db-> fetch_row ($ createtable); $ tabledump. = $ create [1]. "nn"; return $ tabledump;} / *** *** backup database structure and all data ****** / / * function name: data2sql () function function: the table structure and data into SQL function parameters: $ table: to be extracted table name return value : return the result of the extraction, SQL aggregate functions author: heiyeluren * / function data2sql ($ table) {global $ db; $ tabledump = "DROP TABLE IF EXISTS $ table; n"; $ createtable = $ db-> query ("SHOW CREATE TABLE $ table"); $ create = $ db $ rows = $ db-> query ("SELECT * FROM $ table"); $ numfields = $ db-> num_fields ($ CREATE TABLE) $ rows = $ db-> num_rows ($ rows); while ($ row = $ db-> fetch_row ($ rows)) {$ comma = ""; $ tabledump. = "INSERT INTO $ table VALUES ( mysql_escape_string ($ row [$ i]). '' "; $ comma =", "" $ ($ i = 0; $ i <$ numfields; $ i ++) {$ tabledump. = $ comma. "' ;} $ tabledump. = "); n";} $ tabledump. = "n"; return $ tabledump;}?> <! -? php $ host = "localhost"; // host Name $ user = "root"; // MYSQL user name $ password = "root"; // password $ dbname = "dedecmsv4"; // backup database mysql_connect ($ host, $ user, $ password); mysql_select_db $ q1 = mysql_query ("show table"); while ($ t = mysql_fetch_array ($ q1)) {$ table = $ t [0]; $ q2 = mysql_query ; $ mysql. = $ sql ['Create Table']. "; rnrn"; # DDL $ q3 = mysql_query ("select * from` $ table` "); while ($ data $ keys = array_keys ($ data); $ keys = array_map ('addslashes', $ keys); $ keys = join ('`,`', $ keys); $ keys = "` $ vals = array_values ($ data); $ vals = array_map ('addslashes', $ vals); $ vals = join ("','", $ vals); $ vals = " $ vals. "" "$ mysql. =" insert into $ table` ($ keys) values ($ vals); rn ";} $ mysql. =" rn ";} $ filename = date ('Ymd'). "_". $ dbname. ". sql"; // filename is the date of the day $ fp = fopen ($ filename, 'w'); fputs ($ fp, $ mysql); fclose ($ fp); echo "Data backup success, generate a backup file." $ filename;?>