Share a mysql database backup class with detailed call methods. For more information, see. A php mysql database backup class. 1. mysql database backup class backdata. class. php:
Mysql_link = $ mysql_link;} public function backupTables ($ dbName, $ dataDir, $ tableNames) {// start backup $ this-> dbName = $ dbName; $ this-> dataDir = $ dataDir; $ this-> tableNames = $ tableNames; $ tables = $ this-> delarray ($ this-> tableNames); $ sqls = ''; foreach ($ tables as $ tablename) {if ($ tablename = '') {// when the table does not exist, continue ;} ****** * ******* // if a table exists, delete $ sqls first. = "DROP TABLE IF EXISTS $ Tablename; \ n "; // read TABLE structure $ rs = mysql_query (" show create table $ tablename ", $ this-> mysql_link); $ row = mysql_fetch_row ($ rs ); // Obtain the table structure as SQL $ sqls. = $ row ['1']. "; \ n"; unset ($ rs); unset ($ row ); // ************************ the second half of the SQL statement is formed ******* * ***** // query all data in the table $ rs = mysql_query ("select * from $ tablename ", $ this-> mysql_link); // The number of fields in the table $ field = mysql_num_fields ($ rs); // form this SQL statement: "INSERT INTO 'groups' VALUES ('123' E0ca25988d ', 'Directory', '', '0');" while ($ rows = mysql_fetch_row ($ rs) {$ comma = ''; // comma $ sqls. = "insert into '$ tablename' VALUES ("; for ($ I = 0; $ I <$ field; $ I ++) {$ sqls. = $ comma. "'". $ rows [$ I]. "'"; $ comma =', ';} $ sqls. = "); \ n" ;}}$ backfilepath = $ this-> dataDir. date ("Ymdhis", time ()). '. SQL '; // write the file $ filehandle = fopen ($ backfilepath, "w"); fwrite ($ filehandle, $ sqls); fclose ($ filehandle);} private function delarray ($ Array) {// process the imported array foreach ($ array as $ tables) {if ($ tables = '*') {// All tables (an array cannot be formed in the conventional way when the table name is obtained) $ newtables = mysql_list_tables ($ this-> dbName, $ this-> mysql_link ); $ tableList = array (); for ($ I = 0; $ I <mysql_numrows ($ newtables); $ I ++) {array_push ($ tableList, mysql_tablename ($ newtables, $ I);} $ tableList = $ tableList;} else {$ tableList = $ array; break ;}return $ tableList ;}?> 2. call method: Require_once ("backdata. class. php "); $ link = @ mysql_connect (" localhost "," database name "," password ") or die ('could not connect to server. '); mysql_query ("use cms", $ link); mysql_query ("set names utf8", $ link); $ dbbck = new backupData ($ link ); // instantiate it. you only need a link id. // when you back up data, to back up all the tables in a database, you can write as follows: $ dbbck-> backupTables ("cms ",". /", array ('*'); // when backing up data, if you want to back up only one table in a database, you can write as follows: $ dbbck-> backupTables ("cms ",". /", array ('user'); // when backing up data, if you want to back up multiple tables in a database, you can write as follows: $ dbbck-> backupTables ("cms ",". /", array ('user', 'acl', 'informatoin'); // annotation: $ dbbck-> backupTables (" parameter 1 "," parameter 2 ", in array ();, parameter 1 is the database name, parameter 2 is the location where the backup data is to be stored (that is, the directory address), and the third is the tables you want to save |