php mysql数据库备份与数据还原类
说明,该类适用于小型的网站的数据库备份,内置MYSQL连接,只需要简单配置数据连接 * 及存贮备份的位置即可。 * 类实列化并且连接数据库以后可执行以下操作
php教程 mysql教程数据库教程备份与数据还原类
/**
* 说明,该类适用于小型的网站的数据库备份,内置mysql连接,只需要简单配置数据连接
* 及存贮备份的位置即可。
* 类实列化并且连接数据库以后可执行以下操作
* get_db_table($database) 取得所有数据表
* export_sql($table,$subsection=0)) 生成sql文件,注意生成sql文件只保存到服务器目录,不提供下载
* import_sql($dir) 恢复数据只导入服务器目录下的sql文件
* 该类制作简单,可任意传播,如何您对该类有什么提议,请发送邮件给小虾
* @author 赵红健[游天小虾]
* email:328742379@qq.com
* qq交流群:69574955 聚义堂-网页制作交
*/
class data {
public $data_dir = "class/"; //备份文件存放的路径
public $transfer =""; //临时存放sql[切勿不要对该属性赋值,否则会生成错误的sql语句]/**
*数据库连接
*@param string $host 数据库主机名
*@param string $user 用户名
*@param string $pwd 密码
*@param string $db 选择数据库名
*@param string $charset 编码方式
*/
function connect_db($host,$user,$pwd,$db,$charset='gbk'){
if(!$conn = mysql_connect($host,$user,$pwd)){
return false;
}
mysql_select_db($db);
mysql_query("set names $charset");
return true;
}/**
* 生成sql语句
* @param $table 要备份的表
* @return $tabledump 生成的sql语句
*/
public function set_sql($table,$subsection=0,&$tabledom=''){
$tabledom .= "drop table if exists $tablen";
$createtable = mysql_query("show create table $table");
$create = mysql_fetch_row($createtable);
$create[1] = str_replace("n","",$create[1]);
$create[1] = str_replace("t","",$create[1]);$tabledom .= $create[1].";n";
$rows = mysql_query("select * from $table");
$numfields = mysql_num_fields($rows);
$numrows = mysql_num_rows($rows);
$n = 1;
$sqlarry = array();
while ($row = mysql_fetch_row($rows)){
$comma = "";
$tabledom .= "insert into $table values(";
for($i = 0; $i {
$tabledom .= $comma."'".mysql_escape_string($row[$i])."'";
$comma = ",";
}
$tabledom .= ")n";
if($subsection != 0 && strlen($this->transfer )>=$subsection*1000){
$sqlarry[$n]= $tabledom;
$tabledom = ''; $n++;
}
}
return $sqlarry;
}/**
*列表数据库中的表
*@param database $database 要操作的数据库名
*@return array $dbarray 所列表的数据库表
*/
public function get_db_table($database){
$result = mysql_list_tables($database);
while($tmparry = mysql_fetch_row($result)){
$dbarry[] = $tmparry[0];
}
return $dbarry;
}/**
*验证目录是否有效
*@param diretory $dir
*@return booln
*/
function check_write_dir($dir){
if(!is_dir($dir)) {@mkdir($dir, 0777);}
if(is_dir($dir)){
if($link = opendir($dir)){
$filearry = scandir($dir);
for($i=0;$iif($filearry[$i]!='.' || $filearry != '..'){
@unlink($dir.$filearry[$i]);
}
}
}
}
return true;
}
/**
*将数据写入到文件中
*@param file $filename 文件名
*@param string $str 要写入的信息
*@return booln 写入成功则返回true,否则false
*/
private function write_sql($filename,$str){
$re= true;
if(!@$fp=fopen($filename,"w+")) {$re=false; echo "在打开文件时遇到错误,备份失败!";}
if(!@fwrite($fp,$str)) {$re=false; echo "在写入信息时遇到错误,备份失败!";}
if(!@fclose($fp)) {$re=false; echo "在关闭文件 时遇到错误,备份失败!";}
return $re;
}/**
*生成sql文件
*@param string $sql sql 语句
*@param number $subsection 分卷大小,以kb为单位,为0表示不分卷
*/
public function export_sql($table,$subsection=0){
if(!$this->check_write_dir($this->data_dir)){echo '您没有权限操作目录,备份失败';return false;}
if($subsection == 0){
if(!is_array($table)){
$this->set_sql($table,0,$this->transfer);
}else{
for($i=0;$i$this->set_sql($table[$i],0,$this->transfer);
}
}
$filename = $this->data_dir.date("ymd",time()).'_all.sql';
if(!$this->write_sql($filename,$this->transfer)){return false;}
}else{
if(!is_array($table)){
$sqlarry = $this->set_sql($table,$subsection,$this->transfer);
$sqlarry[] = $this->transfer;
}else{
$sqlarry = array();
for($i=0;$i$tmparry = $this->set_sql($table[$i],$subsection,$this->transfer);
$sqlarry = array_merge($sqlarry,$tmparry);
}
$sqlarry[] = $this->transfer;
}
for($i=0;$i$filename = $this->data_dir.date("ymd",time()).'_part'.$i.'.sql';
if(!$this->write_sql($filename,$sqlarry[$i])){return false;}
}
}
return true;
}
/**
*载入sql文件
*@param diretory $dir
*@return booln
*注意:请不在目录下面存放其它文件,或者目录
*以节省恢复时间
*/
public function import_sql($dir){
if($link = opendir($dir)){
$filearry = scandir($dir);
$pattern = "_part[0-9]+.sql$|_all.sql$";
for($i=0;$iif(eregi($pattern,$filearry[$i])){
$sqls=file($dir.$filearry[$i]);
foreach($sqls as $sql){
str_replace("r","",$sql);
str_replace("n","",$sql);
if(!mysql_query(trim($sql))) return false;
}
}
}
return true;
}
}}
应用方法
//$d = new data();
//连接数据库
//if(!$d->connect_db('localhost','root','','guestbook','gbk')){
// echo '数据库连接失败';
//}//查找数据库内所有数据表
//$tablearry = $d->get_db_table('guestbook');//备份并生成sql文件
//if(!$d->export_sql($tablearry)){
// echo '备份失败';
//}else{
// echo '备份成功';
//}//恢复导入sql文件夹
//if($d->import_sql($d->data_dir)){
// echo '恢复成功';
//}else{
// echo '恢复失败';
//}

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
