For the database backup class, we only need to search for more than N class code on the internet. below I will summarize several good database backup classes, hoping to help you. database Backup class. use the following code: require_once (quot; backdata. CIA... for the database backup class, we only need to search for more than N class code on the internet. below I will summarize several good database backup classes, hoping to help you.
Database Backup class. the code is as follows:
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 ", array,
Parameter 1: database name
Parameter 2: location of the backup data to be stored (that is, the directory address)
Third: the tables you want to save
The backdata. class. php code is as follows:
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 to form SQL $ sqls. = $ row ['1']. "; nn"; 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); // number of fields in the table $ field = mysql_num_fields ($ rs); // this SQL statement is formed: "insert into 'groups' VALUES ('1499e0ca25988d', '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. = "); nnn" ;}}$ 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 ;}}
Address:
Reprinted at will, but please attach the article address :-)