PHP MySQL database backup class and calling method

Source: Internet
Author: User
Tags php mysql
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
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.