国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP Mysql數(shù)據(jù)庫備份類程序總結(jié)

2024-05-04 21:49:10
字體:
供稿:網(wǎng)友

數(shù)據(jù)庫備份類我們只要在網(wǎng)上一搜索在N多的類代碼,下面我來總結(jié)幾款不錯數(shù)據(jù)庫備份希望對大家有所幫助.

數(shù)據(jù)庫備份類,使用方法,代碼如下:

  1. require_once("backdata.class.php"); 
  2. $link = @mysql_connect("localhost","數(shù)據(jù)庫名","密碼"or die ('Could not connect to server.'); 
  3. mysql_query("use cms",$link); 
  4. mysql_query("set names utf8",$link); 
  5. //開源代碼Vevb.com 
  6. $dbbck = new backupData($link);//實例化它,只要一個鏈接標(biāo)識就行了 
  7.  
  8. //備份數(shù)據(jù)時,如想備份一個數(shù)據(jù)庫中的所有表,你可這樣寫: 
  9. $dbbck->backupTables("cms","./",array('*')); 
  10.  
  11. //備份數(shù)據(jù)時,如想備份一個數(shù)據(jù)庫中的僅一個表時,你可這樣寫: 
  12. $dbbck->backupTables("cms","./",array('user')); 
  13.  
  14. //備份數(shù)據(jù)時,如想備份一個數(shù)據(jù)庫中的多個表時,你可這樣寫: 
  15. $dbbck->backupTables("cms","./",array('user','acl','informatoin')); 
  16.  
  17. //注解:$dbbck->backupTables("參1","參2",array());中, 

參1為:數(shù)據(jù)庫名

參2為:要存放備份數(shù)據(jù)的位置(即目錄地址)

第三個為:你要保存那些表

backdata.class.php,代碼如下:

  1. <?php 
  2. /* 
  3. * 
  4. *一個簡單的Mysql備份數(shù)據(jù)類 
  5. * 
  6. */ 
  7. class backupData{ 
  8.     private    $mysql_link;//鏈接標(biāo)識 
  9.     private    $dbName;    //數(shù)據(jù)庫名 
  10.     private    $dataDir;     //數(shù)據(jù)所要存放的目錄 
  11.     private    $tableNames;//表名 
  12.  
  13.     public function __construct($mysql_link){ 
  14.          $this->mysql_link = $mysql_link
  15.     } 
  16.     public function backupTables($dbName,$dataDir,$tableNames){//開始備份 
  17.         $this->dbName  = $dbName
  18.         $this->dataDir  = $dataDir
  19.         $this->tableNames = $tableNames
  20.         $tables=$this->delarray($this->tableNames); 
  21.         $sqls=''
  22.         foreach($tables as $tablename){ 
  23.             if($tablename==''){//表不存在時 
  24.                 continue
  25.             } 
  26.              
  27.             //************************以下是形成SQL的前半部分************** 
  28.             //如果存在表,就先刪除 
  29.             $sqls .= "DROP TABLE IF EXISTS $tablename;n"
  30.             //讀取表結(jié)構(gòu) 
  31.             $rs = mysql_query("SHOW CREATE TABLE $tablename",$this->mysql_link);   
  32.             $row=mysql_fetch_row($rs); 
  33.             //獲得表結(jié)構(gòu)組成SQL 
  34.             $sqls.=$row['1'].";nn"
  35.             unset($rs); 
  36.             unset($row); 
  37.              
  38.             //************************以下是形成SQL的后半部分************** 
  39.             //查尋出表中的所有數(shù)據(jù) 
  40.             $rs=mysql_query("select * from $tablename",$this->mysql_link); 
  41.             //表的字段個數(shù) 
  42.             $field=mysql_num_fields($rs); 
  43.             //形成此種SQL語句:"INSERT INTO `groups` VALUES('1499e0ca25988d','主任','','0');" 
  44.             while($rows=mysql_fetch_row($rs)){ 
  45.                 $comma='';//逗號 
  46.                 $sqls.="INSERT INTO `$tablename` VALUES("
  47.                 for($i=0;$i<$field;$i++){ 
  48.                     $sqls.=$comma."'".$rows[$i]."'"
  49.                     $comma=','
  50.                 } 
  51.                 $sqls.=");nnn"
  52.             } 
  53.         } 
  54.         $backfilepath=$this->dataDir.date("Ymdhis",time()).'.sql'
  55.          
  56.         //寫入文件 
  57.         $filehandle = fopen($backfilepath"w"); 
  58.         fwrite($filehandle$sqls); 
  59.         fclose($filehandle); 
  60.     } 
  61.     private function delarray($array){    //處理傳入進(jìn)來的數(shù)組 
  62.         foreach($array as $tables){ 
  63.             if($tables=='*'){    //所有的表(獲得表名時不能按常規(guī)方式來組成一個數(shù)組) 
  64.                 $newtables=mysql_list_tables($this->dbName,$this->mysql_link); 
  65.                 $tableList = array(); 
  66.                 for ($i = 0; $i < mysql_numrows($newtables); $i++){ 
  67.                     array_push($tableList,mysql_tablename($newtables$i)); 
  68.                 } 
  69.                 $tableList=$tableList
  70.             }else
  71.                 $tableList=$array
  72.                 break
  73.             } 
  74.         } 
  75.         return $tableList
  76.     } 
  77. ?>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 榕江县| 蒲城县| 明溪县| 墨竹工卡县| 龙陵县| 汾阳市| 镇原县| 定安县| 新河县| 玉溪市| 石狮市| 滕州市| 尖扎县| 正镶白旗| 乌鲁木齐市| 永顺县| 杂多县| 南宫市| 庄河市| 和林格尔县| 称多县| 柳江县| 德阳市| 宜州市| 新郑市| 讷河市| 屯门区| 衡阳县| 东宁县| 洛扎县| 成武县| 黑水县| 兴隆县| 平远县| 尖扎县| 翼城县| 宜章县| 贺兰县| 胶州市| 龙泉市| 青岛市|