欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php MYSQL 數(shù)據(jù)備份類

 更新時(shí)間:2009年06月19日 21:41:25   作者:  
一個(gè)簡單MYSQL的數(shù)據(jù)備份類 這些一直都在搞數(shù)據(jù),因此數(shù)據(jù)的備份就少不了的了,如果不寫這類一個(gè)簡單MYSQL的數(shù)據(jù)備份類,那將是很麻煩的。自己就下定決心,寫了一個(gè)。
功能上有: ­
require_once("backdata.class.php");
$link = @mysql_connect("localhost","數(shù)據(jù)庫名","密碼") or die ('Could not connect to server.');
mysql_query("use cms",$link);
mysql_query("set names utf8",$link);
$dbbck=new backupData($link);//實(shí)例化它,只要一個(gè)鏈接標(biāo)識就行了
//備份數(shù)據(jù)時(shí),如想備份一個(gè)數(shù)據(jù)庫中的所有表,你可這樣寫:
$dbbck->backupTables("cms","./",array('*'));
­
//備份數(shù)據(jù)時(shí),如想備份一個(gè)數(shù)據(jù)庫中的僅一個(gè)表時(shí),你可這樣寫:
$dbbck->backupTables("cms","./",array('user'));
­
//備份數(shù)據(jù)時(shí),如想備份一個(gè)數(shù)據(jù)庫中的多個(gè)表時(shí),你可這樣寫:
­
$dbbck->backupTables("cms","./",array('user','acl','informatoin'));
//注解:$dbbck->backupTables("參1","參2",array());中,
參1為:數(shù)據(jù)庫名,
參2為:要存放備份數(shù)據(jù)的位置(即目錄地址)
第三個(gè)為:你要保存那些表
ok...
­
以下為代碼:
復(fù)制代碼 代碼如下:

<?php
/*
*
*簡單的一個(gè)備份數(shù)據(jù)類
*author FC
*
*/
class backupData{
private $mysql_link;//鏈接標(biāo)識
private $dbName;//數(shù)據(jù)庫名
private $dataDir; //數(shù)據(jù)所要存放的目錄
private $tableNames;//表名
public function __construct($mysql_link){
­
$this->mysql_link = $mysql_link;
}
­
public function backupTables($dbName,$dataDir,$tableNames){//開始備份
­
$this->dbName = $dbName;
$this->dataDir = $dataDir;
$this->tableNames = $tableNames;
$tables=$this->delarray($this->tableNames);
$sqls='';
foreach($tables as $tablename){
if($tablename==''){//表不存在時(shí)
continue;
}
//************************以下是形成SQL的前半部分**************
//如果存在表,就先刪除
$sqls .= "DROP TABLE IF EXISTS $tablename;\n";
//讀取表結(jié)構(gòu)
$rs = mysql_query("SHOW CREATE TABLE $tablename",$this->mysql_link);
$row=mysql_fetch_row($rs);
//獲得表結(jié)構(gòu)組成SQL
$sqls.=$row['1'].";\n\n";
unset($rs);
unset($row);
//************************以下是形成SQL的后半部分**************
//查尋出表中的所有數(shù)據(jù)
$rs=mysql_query("select * from $tablename",$this->mysql_link);
//表的字段個(gè)數(shù)
$field=mysql_num_fields($rs);
//形成此種SQL語句:"INSERT INTO `groups` VALUES('1499e0ca25988d','主任','','0');"
while($rows=mysql_fetch_row($rs)){
$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';
//寫入文件
$filehandle = fopen($backfilepath, "w");
fwrite($filehandle, $sqls);
fclose($filehandle);
}
­
private function delarray($array){//處理傳入進(jìn)來的數(shù)組
foreach($array as $tables){
if($tables=='*'){//所有的表(獲得表名時(shí)不能按常規(guī)方式來組成一個(gè)數(shù)組)
$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;
}
}

相關(guān)文章

最新評論