PHP Mysql数据库备份类程序概括
发布时间:2022-07-05 15:55:09 所属栏目:PHP教程 来源:互联网
导读:数据库备份类我们只要在网上一搜索在N多的类代码,下面我来总结几款不错数据库备份希望对大家有所帮助. 数据库备份类,使用方法,代码如下: require_once(backdata.class.php); $link = @mysql_connect(localhost,数据库名,密码) or die (Could not connect to s
数据库备份类我们只要在网上一搜索在N多的类代码,下面我来总结几款不错数据库备份希望对大家有所帮助. 数据库备份类,使用方法,代码如下: 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); //开源代码phpfensi.com //备份数据时,如想备份一个数据库中的多个表时,你可这样写: $dbbck->backupTables("cms","./",array('user','acl','informatoin')); //注解:$dbbck->backupTables("参1","参2",array());中, 参1为:数据库名 参2为:要存放备份数据的位置(即目录地址) 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==''){//表不存在时 continue; } //************************以下是形成SQL的前半部分************** //如果存在表,就先删除 unset($row); //************************以下是形成SQL的后半部分************** //查寻出表中的所有数据 $rs=mysql_query("select * from $tablename",$this->mysql_link); //表的字段个数 $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.=");nnn"; } } $backfilepath=$this->dataDir.date("Ymdhis",time()).'.sql'; //写入文件 $filehandle = fopen($backfilepath, "w"); fwrite($filehandle, $sqls); fclose($filehandle); } for ($i = 0; $i < mysql_numrows($newtables); $i++){ array_push($tableList,mysql_tablename($newtables, $i)); } $tableList=$tableList; }else{ $tableList=$array; break; } } return $tableList; } } ?> (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |