search
Homephp教程php手册mysql 数据备份类代码

mysql 数据备份类代码

Jun 13, 2016 am 10:07 AM
mysqlcodeInsidebackupdata backupdatabaseofkindnetillustrate

mysql 数据备份类代码 /*** * 说明,该类适用于小型的网站的数据库备份,内置MYSQL连接,只需要简单配置数据连接 * 及存贮备份的位置即可。 * 类中show_dir_file() 方法可直接返回备份目录下的所有文件,返回以数组形式

mysql教程 数据备份类代码
 /***
  * 说明,该类适用于小型的网站的数据库教程备份,内置mysql连接,只需要简单配置数据连接
  * 及存贮备份的位置即可。
  * 类中show_dir_file() 方法可直接返回备份目录下的所有文件,返回以数组形式
  * 方法 expord_sql() 直接生成sql文件
  * 该类制作简单,可任意传播,如何您对该类有什么提议,请发送邮件给小虾
  * 制作人:游天小虾
  * emial:328742379@qq.com
  * **/

class data {
 public   $data_dir    = "class/";     //备份文件存放的路径
 public   $data_name   = "111cnnet.sql";   //备份文件名
 private  $mysql_host  = "localhost";    //数据库地址
 private  $mysql_user  = "root";     //用户名
 private  $mysql_pwd   = "lpl19881129";    //密码
 private  $mysql_db    = "date";     //数据库名
 private  $mysql_code  = "gbk";      //编码方式

/***
 * 1.连接数据库
 * **/
 function __construct(){
  $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pwd);
  mysql_select_db($this->mysql_db);
  mysql_query("set names $this->mysql_code");
 }

/***
 * 2.生成sql语句
 * **/
 private function set_sql($table){
  $tabledump = "drop table if exists $table;n";
  $createtable = mysql_query("show create table $table");
  $create = mysql_fetch_row($createtable);
  $tabledump .= $create[1].";nn";

  $rows = mysql_query("select * from $table");
  $numfields = mysql_num_fields($rows);
  $numrows = mysql_num_rows($rows);
  while ($row = mysql_fetch_row($rows)){
     $comma = "";
     $tabledump .= "insert into $table values(";
     for($i = 0; $i      {
    $tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
    $comma = ",";
     }
     $tabledump .= ");n";
  }
  $tabledump .= "n";

  return $tabledump;
   }

/***
 * 3.显示存放目录下已经备份的文件
 * **/
    public function show_dir_file() {
  $dir = $this->data_dir;
     if(!is_dir($dir)){
   if(!mkdir($dir)){
    echo "文件夹不存在,尝试创建文件夹,创建失败,可能是您没有相关权限";
    exit();
   }else{
    chmod($dir,755);
   }
     }

  if(!is_writable($dir)){
   echo " 文件不可写";
   exit();
  }

  $link = opendir($dir);
  if(!$link){
   echo "创建链接失败";
   exit();
  }
  return scandir($dir);
    }

/***
 * 4.生成sql文件
 * **/
    public function expord_sql(){
     $this->show_dir_file();
     $result = mysql_list_tables($this->mysql_db);
  while($arr = mysql_fetch_row($result)){
   $tables .= $this->set_sql($arr[0]);
  }
  $file = $this->data_dir.$this->data_name;
  $link = fopen($file,"w+");
  if(!is_writable($file)){
   echo "文件不可写";
   exit();
  }
  fwrite($link,$tables);
  fclose($link);
  echo "备份成功";
    }

}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools