Home > Backend Development > PHP Tutorial > PHP Mysql database backup program summary_PHP tutorial

PHP Mysql database backup program summary_PHP tutorial

WBOY
Release: 2016-07-13 17:06:47
Original
920 people have browsed it

We only need to search for N types of database backup class codes on the Internet. Now I will summarize a few good database backups and hope it will be helpful to everyone.

Database backup class

How to use:

The code is as follows Copy code
 代码如下 复制代码

 

require_once("backdata.class.php");
$link = @mysql_connect("localhost","数据库名","密码") or die ('Could not connect to server.');
mysql_query("use cms",$link);
mysql_query("set names utf8",$link);

$dbbck = new backupData($link);//实例化它,只要一个链接标识就行了

//备份数据时,如想备份一个数据库中的所有表,你可这样写:
$dbbck->backupTables("cms","./",array('*'));

//备份数据时,如想备份一个数据库中的仅一个表时,你可这样写:
$dbbck->backupTables("cms","./",array('user'));

//备份数据时,如想备份一个数据库中的多个表时,你可这样写:
$dbbck->backupTables("cms","./",array('user','acl','informatoin'));

//注解:$dbbck->backupTables("参1","参2",array());中,

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, just a link identifier is enough //When backing up data, if you want to back up all tables in a database, you can write like this:
$dbbck->backupTables("cms","./",array('*')); //When backing up data, if you want to back up only one table in a database, you can write like this:
$dbbck->backupTables("cms","./",array('user')); //When backing up data, if you want to back up multiple tables in a database, you can write like this:
$dbbck->backupTables("cms","./",array('user','acl','informatoin')); //Note: $dbbck->backupTables("parameter 1", "parameter 2", array());,

Parameter 1 is: database name,
Parameter 2 is: the location where the backup data is to be stored (i.e. directory address)
The third one is: Which tables do you want to save

backdata.class.php

The code is as follows Copy code

/*
*
*A simple Mysql backup data class
*
*/
class backupData{
Private $mysql_link;//Link identification
Private $dbName; //Database name
private $dataDir; //The directory where the data is to be stored
Private $tableNames;//Table name

public function __construct($mysql_link){
             $this->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;
            }
                                                                      ​​​​​​ //**********************The following is the first half of the SQL**************
//If the table exists, delete it first
                 $sqls .= "DROP TABLE IF EXISTS $tablename;n";
//Read table structure
                $rs = mysql_query("SHOW CREATE TABLE $tablename",$this->mysql_link);
                $row=mysql_fetch_row($rs);
//Get the table structure to form SQL
                $sqls.=$row['1'].";nn";
               unset($rs);
               unset($row);
                                                                      //************************The following is the second half of the SQL**************
//Search for 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);
//Form this SQL statement: "INSERT INTO `groups` VALUES('1499e0ca25988d','Director','','0');"
​​​​​​while($rows=mysql_fetch_row($rs)){
$ COMMA = ''; // The 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 file
          $filehandle = fopen($backfilepath, "w");
            fwrite($filehandle, $sqls);
           fclose($filehandle);
}
Private function delarray($array){ //Process the incoming array
foreach($array as $tables){
                                                                                                                                                                                                                                           to ’ ’ to ’ to Receive Table ’s ’ to ’ $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;
}
}
?>

http://www.bkjia.com/PHPjc/630673.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630673.htmlTechArticleDatabase backup class We only need to search for N-type codes on the Internet. Let me summarize a few good databases. I hope the backup will be helpful to everyone. Database backup class usage: Generation...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template