PHP 通用数据库备份类的解决办法
内容摘要
这篇文章主要为大家详细介绍了PHP 通用数据库备份类的简单示例,具有一定的参考价值,可以用来参考一下。
对PHP通用数据库备份类对此感兴趣的朋友,看看idc笔记做的技术笔记! php
对PHP通用数据库备份类对此感兴趣的朋友,看看idc笔记做的技术笔记! php
文章正文
这篇文章主要为大家详细介绍了PHP 通用数据库备份类的简单示例,具有一定的参考价值,可以用来参考一下。
对PHP通用数据库备份类对此感兴趣的朋友,看看idc笔记做的技术笔记! 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,0,-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;
*/
?>
/*** 来自php教程(www.idcnote.com) ***/
注:关于PHP 通用数据库备份类的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释