Recently, I studied how to use php to back up MYSQL on the website background, found related information on the Internet, and then wrote a class library for php to back up MYSQL based on my understanding. Now I will share it with you. Recently, I studied how to use php to back up MYSQL on the website background, found related information on the Internet, and then wrote a class library for php to back up MYSQL based on my understanding. Now I will share it with you.
Script ec (2); script
I just want to study how to back up the database and share a php class library for MYSQL backup.
The Code is as follows: |
|
/****** Back up the database structure ******/ /***** Just want to study how to back up the database and share a php MYSQL backup class library ********/ /* Function Name: table2sql () Function: converts the table structure to SQL Function parameter: $ table: name of the table to be extracted Return Value: return the extracted results, SQL set Function Author: 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; }
/****** Back up the database structure and all data ******/ /* Function Name: data2sql () Function: converts the table structure and data into SQL statements. Function parameter: $ table: name of the table to be extracted Return Value: return the extracted results, SQL set Function Author: heiyeluren */ Function data2sql ($ 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 ";
$ Rows = $ db-> query ("SELECT * FROM $ table "); $ Numfields = $ db-> num_fields ($ rows ); $ Numrows = $ db-> num_rows ($ rows ); While ($ row = $ db-> fetch_row ($ rows )) { $ Comma = ""; $ Tabledump. = "insert into $ table VALUES ("; For ($ I = 0; $ I <$ numfields; $ I ++) { $ Tabledump. = $ comma. "'". mysql_escape_string ($ row [$ I]). "'"; $ Comma = ","; } $ Tabledump. = "); n "; } $ Tabledump. = "n ";
Return $ tabledump; } ?> |
Summary: The principle of this class library is also very simple. It is to read the database tables cyclically, then call the records in the table and output them cyclically.