数据库备份类我们只要在网上一搜索在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); $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());中,
参1为:数据库名
参2为:要存放备份数据的位置(即目录地址)
第三个为:你要保存那些表
backdata.class.php,代码如下:
<?php /* * *一个简单的Mysql备份数据类 * */ class backupData{ private $mysql_link;//链接标识 private $dbName; //数据库名 private $dataDir; //数据所要存放的目录 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==''){//表不存在时 continue; } //************************以下是形成SQL的前半部分************** //如果存在表,就先删除 $sqls .= "DROP TABLE IF EXISTS $tablename;n"; //读取表结构 $rs = mysql_query("SHOW CREATE TABLE $tablename",$this->mysql_link); $row=mysql_fetch_row($rs); //获得表结构组成SQL $sqls.=$row['1'].";nn"; unset($rs); 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); } private function delarray($array){ //处理传入进来的数组 foreach($array as $tables){ if($tables=='*'){ //所有的表(获得表名时不能按常规方式来组成一个数组) $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; } }
本文地址:
转载随意,但请附上文章地址:-)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.