Home > Backend Development > PHP Tutorial > PHP implements MySQL database backup class

PHP implements MySQL database backup class

墨辰丷
Release: 2023-03-31 15:48:01
Original
4472 people have browsed it

The example in this article describes how PHP implements the MySQL database backup class. Share it with everyone for your reference. The specific analysis is as follows: This is a very simple class file that uses php to back up mysql database. We only need to simply configure the connection address, user name and database.

php backup database class sharing

<?php
  /**
  *  
  *  @name php备份数据库 
  *  @param string  $DbHost    连接主机
  *  @param string  $DbUser    用户名
  *  @param string  $DbPwd    连接密码
  *  @param string  $DbName    要备份的数据库
  *  @param string  $saveFileName 要保存的文件名, 默认文件保存在当前文件夹中,以日期作区分
  *  @return Null
  *  @example backupMySqlData(&#39;localhost&#39;, &#39;root&#39;, &#39;123456&#39;, &#39;YourDbName&#39;);
  *
  */
  function backupMySqlData($DbHost, $DbUser, $DbPwd, $DbName, $saveFileName = &#39;&#39;)
  {
    header("Content-type:text/html;charset=utf-8");
    error_reporting(0);
    set_time_limit(0);

    echo &#39;数据备份中,请稍候......<br />&#39;;

    $link = mysql_connect($DbHost, $DbUser, $DbPwd) or die(&#39;数据库连接失败: &#39; . mysql_error());
    mysql_select_db($DbName) or die(&#39;数据库连接失败: &#39; . mysql_error());
    mysql_query(&#39;set names utf8&#39;);
    
    // 声明变量
    $isDropInfo   = &#39;&#39;;
    $insertSQL   = &#39;&#39;;
    $row      = array();
    $tables     = array();
    $tableStructure = array();
    $fileName    = ($saveFileName ? $saveFileName : &#39;MySQL_data_bakeup_&#39;) . date(&#39;YmdHis&#39;) . &#39;.sql&#39;;

    // 枚举该数据库所有的表
    $res = mysql_query("SHOW TABLES FROM $DbName");
    while ($row = mysql_fetch_row($res)) {

      $tables[] = $row[0];

    }
    mysql_free_result($res);

    // 枚举所有表的创建语句
    foreach ($tables as $val) {

      $res = mysql_query("show create table $val", $link);
      $row = mysql_fetch_row($res);

      $isDropInfo   = "DROP TABLE IF EXISTS `" . $val . "`;\r\n";
      $tableStructure = $isDropInfo . $row[1] . ";\r\n";

      file_put_contents($fileName, $tableStructure, FILE_APPEND);
      mysql_free_result($res);
    }

    // 枚举所有表的INSERT语句
    foreach ($tables as $val) {

      $res = mysql_query("select * from $val");

      // 没有数据的表不执行insert
      while ($row = mysql_fetch_row($res)) {

        $sqlStr = "INSERT INTO `".$val."` VALUES (";

        foreach($row as $v){

          $sqlStr .= "&#39;$v&#39;,";

        }
        //去掉最后一个逗号
        $sqlStr = substr($sqlStr, 0, strlen($sqlStr) - 1);
        $sqlStr .= ");\r\n";

        file_put_contents($fileName, $sqlStr, FILE_APPEND);
      }
      mysql_free_result($res);
    }

    echo &#39;数据备份成功!&#39;;
  }
   // 调用此方法 
  backupMySqlData(&#39;localhost&#39;, &#39;root&#39;, &#39;123456&#39;, &#39;YouDbName&#39;);  
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

phpHow to convert numbers into Chinese characters

PHP Mysql jQuery implements password retrieval Function

php method to dynamically create html code based on array

The above is the detailed content of PHP implements MySQL database backup class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template