PHP 通用数据库备份类的简单示例

前端之家收集整理的这篇文章主要介绍了PHP 通用数据库备份类的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP通用数据库备份类感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧! PHP代码如下:

 <?PHP
 /*数据库备份:NOTICE:此类要添加数据库连接才能正常工作,此类继承了一个数据库操作的类
*/
Class Back_up_database extends dbstuff{
//类开始
var $HOST;
var $USERNAME;
var $PASSWORD;
var $DATABASE;
function Back_up_database($host,$username,$password,$database){
//初始化数据库连接
$this->HOST=$host;
$this->USERNAME=$username;
$this->PASSWORD=$password;
$this->DATABASE=$database;
$Connection=$this->connect($this->HOST,$this->USERNAME,$this->PASSWORD,$this->DATABASE,$pconnect);
$this->Connection=$Connection;
}
//取得数据库中的表
function get_table_name($database){
$this->Connection;
$result=MysqL_list_tables($database);
$i=0;
while($i<MysqL_num_rows($result)){
$tb_name[$i]=MysqL_tablename($result,$i);
$table_name.= $tb_name[$i].",";
$i++;
}
$this->table_name=substr($table_name,-1);
return $this->table_name;
}
//取得每个表中的FIELDS和属性生成CREATE TABLE语句
function get_table_fields($table_name){
$this->Connection;
$createtable=dbstuff::query("SHOW CREATE TABLE $table_name");
$create=dbstuff::fetch_row($createtable);
$tabledump .="DROP TABLE IF EXISTS $table_name;\n";
$tabledump .= $create[1].";\n\n";
$this->$table_name=$tabledump;
return $this->$table_name;
}
//取得表中的数据并生成ISERT INTO 语句
function get_insert($table_insert_name){
$this->Connection;
$rows = dbstuff::query("SELECT * FROM $table_insert_name");
$numfields = dbstuff::num_fields($rows);
$numrows = dbstuff::num_rows($rows);
while ($row = dbstuff::fetch_row($rows)) {
$comma = "";
$tabledump .= "INSERT INTO $table_insert_name VALUES(";
for($i = 0; $i < $numfields; $i++) {
$tabledump .= $comma."'".MysqL_escape_string($row[$i])."'";
$comma = ",";
}
$tabledump .= ");\n";
}
$this->tabledump=$tabledump;
return $this->tabledump;
}
//获取所有数据并连接成新的字符串并将它写入文件中.sql
function get_string($database_name,$file_path_name){
$time=date("Y-m-d H:j");
$date_time=date("YmdHis");
$file_path_name=$file_path_name.$date_time.".sql";
$version="Antsent_Web_Studio Database BackUp V1.01";
$idstring = '# Identify: '.base64_encode("$time,$version")."\n";
$head_info="$idstring".
"#\n".
"# Antsnet_Web! The Basic Class Of BackUp DataBase\n".
"# Version: Antsnet Web! $version\n".
"# Time: $time\n".
"# Type: Class Of BackUp DataBase\n".
"# Antsnet_Web_Studio! Home: http://www.antsnet.net\n".
"# Please visit our website for newest infomation about Antsnet_Web_Studio!\n".
"# --------------------------------------------------------\n\n\n";
$table_name=$this->get_table_name($database_name);
$array_table=explode(",",$table_name);
for($i=0;$i<count($array_table);$i++){
$table_string.=$this->get_table_fields($array_table[$i]);
$table_insert.=$this->get_insert($array_table[$i]);
}
$count_string=$head_info.$table_string.$table_insert;
//return $count_string;
$write_status=$this->write_file($file_path_name,$count_string);
return $write_status;
}
//写入一个文件
function write_file($file_path,$file_contents){
if(@!$fp=fopen($file_path,'w')){
$status="<font color=\"red\">This File Could Not Open Or Read.</font>";
}else{
flock($fp,3);
fwrite($fp,$file_contents);
fclose($fp);
$status="Write TO The File Success.Success To Backup The Database.";
}
return $status;
}
//读取文件
function read_file($file_path){
if(@!$fp= fopen($file_path,'r')){
$status="<font color=\"red\">This File Could Not Open Or Read.</font>";
}else{
$status=fread($fp,filesize($file_path));
fclose($fp);
}
return $status;
}
//分离sql语句
function splitsql($sql){
$sql = str_replace("\r","\n",$sql);
$ret = array();
$num = 0;
$queriesarray = explode(";\n",trim($sql));
unset($sql);
foreach($queriesarray as $query) {
$queries = explode("\n",trim($query));
foreach($queries as $query) {
$ret[$num] .= $query[0] == "#" ? NULL : $query;
}
$num++;
}
return($ret);
}
//恢复数据
function db_restore($file_restore){
$content=$this->read_file($file_restore);
$this->Connection;
$sqlquery =$this->splitsql($content);
unset($content);
foreach($sqlquery as $sql) {
if(trim($sql) != '') {
$status=dbstuff::query($sql);
}
}
if(!status){
$msg="Import False.";
}else{
$msg="Import Success.";
}
return $msg;
}
//类结束
}


/*具体用法:
$bak=new Back_up_database("dbserver","dbusername","dbpassword","db");
//$status=$bak->get_string("advertisment");
$str=$bak->get_string("advertisment","bak.sql");
echo $str;
*/
?>
/*** 来自编程之家 jb51.cc(jb51.cc) ***/
原文链接:https://www.f2er.com/php/527990.html

猜你在找的PHP相关文章