Share a MySQL database backup class, and attach a detailed call method, with a friend in need of reference. A PHP MySQL database backup class, shared to everyone. 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 = = ") {//table does not exist when continue; //************************ The following is the first half of the form of SQL **************//If the table is present, delete the $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); Gets the table structure that makes up the SQL $sqls. = $row [' 1 ']. "; N\n "; Unset ($RS); Unset ($row); The following is the second half of the SQL **************//lookup of all the data in the table $rs =mysql_query ("SELECT * from $t Ablename ", $this->mysql_link); The number of fields in the table $field =mysql_num_fields ($RS); form this type of SQL statement: "INSERT into ' groups ' VALUES (' 1499e0ca25988d ', ' director ', ' ', ' 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\n\n"; }} $backfilepath = $this->datadir.date ("Ymdhis", Time ()). SQL '; Write File $filehandle = fopen ($backfilepath, "w"); Fwrite ($filehandle, $SQLS); Fclose ($filehandle); } Private Function Delarray ($array) {//Process incoming array foreach ($array as $tables) {if ($tables = = ' * ') { All tables (the table name cannot be formed in the usual way for an array) $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 as long as a link is identified on the line// To back up your data, if you want to back up all the tables in a database, you can write: $dbbck->backuptables ("CMS", "./", Array (' * '));//When backing up data, you can write it like this if you want to back up only one table in a database: $ Dbbck->backuptables ("CMS", "./", Array (' user '));//When backing up data, if you want to back up multiple tables in a database, you can write: $dbbck->backuptables ("CMS", " ./", Array (' user ', ' ACL ', ' informatoin '));//Note: $dbbck->backuptables (" Parameter 1 "," Parameter 2 ", Array ()), and Parameter 1: database name, Parameter 2 is: the location where the backup data is to be stored (that is, the directory address) The third is: you want to save those tables |