Home > php教程 > php手册 > body text

php备份数据库类分享,php备份数据库

WBOY
Release: 2016-06-13 09:07:06
Original
731 people have browsed it

php备份数据库类分享,php备份数据库

php备份数据库类分享

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

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

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

    // 枚举该数据库所有的表
    $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 .= "'$v',";

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

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

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

以上所述就是本文的全部内容了,希望大家能够喜欢。

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!