php备份数据库类分享

前端之家收集整理的这篇文章主要介绍了php备份数据库类分享前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

PHP备份数据库分享

PHP;"> 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 = <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_connect($DbHost,$DbPwd) or die('<a href="https://www.jb51.cc/tag/shujuku/" target="_blank" class="keywords">数据库</a>连接失败: ' . <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_error());
<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_select_db($DbName) or die('<a href="https://www.jb51.cc/tag/shujuku/" target="_blank" class="keywords">数据库</a>连接失败: ' . <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_error());
<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query('set names utf8');

// 声明变量
$isDropInfo   = '';
$insert<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>   = '';
$row      = array();
$tables     = array();
$tableStructure = array();
$fileName    = ($saveFileName ? $saveFileName : '<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_data_bakeup_') . date('YmdHis') . '.<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>';

// 枚举该<a href="https://www.jb51.cc/tag/shujuku/" target="_blank" class="keywords">数据库</a>所有的表
$res = <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("SHOW TABLES FROM $DbName");
while ($row = <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_fetch_row($res)) {

  $tables[] = $row[0];

}
<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_free_result($res);

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

  $res = <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("show create table $val",$link);
  $row = <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_fetch_row($res);

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

  file_put_contents($fileName,$tableStructure,FILE_APPEND);
  <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_free_result($res);
}

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

  $res = <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("select * from $val");

  // 没有数据的表不执行insert
  while ($row = <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_fetch_row($res)) {

    $<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>Str = "INSERT INTO `".$val."` VALUES (";

    foreach($row as $v){

      $<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>Str .= "'$v',";

    }
    //去掉最后一个逗号
    $<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>Str = substr($<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>Str,strlen($<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>Str) - 1);
    $<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>Str .= ");\r\n";

    file_put_contents($fileName,$<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>Str,FILE_APPEND);
  }
  <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_free_result($res);
}

echo '数据备份成功!';

}
   // 调用方法
backupMysqLData('localhost','YouDbName');
?>

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

原文链接:https://www.f2er.com/php/22131.html

猜你在找的PHP相关文章