Home >php教程 >php手册 >分享PHP备份MYSQL数据的两种方法

分享PHP备份MYSQL数据的两种方法

PHPz
PHPzOriginal
2016-06-06 20:22:251239browse

通常情况下,我们都是使用工具备份,比如phpmyadmin、navicat。如果需要使用PHP备份,如何实现呢

下面提供两种方法,仅供研究使用。

第1种方法:

代码如下:

<?php
$host="localhost";
$user="root";
$password="";
$dbname="dbname";
mysql_connect($host,$user,$password);
mysql_select_db($dbname);
$mysql= "set names utf8;";
mysql_query($mysql);
$q1=mysql_query("show tables");
while($t=mysql_fetch_array($q1)){
$table=$t[0];
$q2=mysql_query("show create table `$table`");
$sql=mysql_fetch_array($q2);
$mysql.=$sql[&#39;Create Table&#39;].";\n";
$q3=mysql_query("select * from `$table`");
while($data=mysql_fetch_assoc($q3)){
$keys=array_keys($data);
$keys=array_map(&#39;addslashes&#39;,$keys);
$keys=join(&#39;`,`&#39;,$keys);
$keys="`".$keys."`";
$vals=array_values($data);
$vals=array_map(&#39;addslashes&#39;,$vals);
$vals=join("&#39;,&#39;",$vals);
$vals="&#39;".$vals."&#39;";
$mysql.="insert into `$table`($keys) values($vals);\n";
}
$mysql.="\n";
}
$filename=$dbname.date(&#39;Ymj&#39;).".sql";
$fp = fopen($filename,&#39;w&#39;);
fputs($fp,$mysql);
fclose($fp);
echo "数据备份成功,生成备份文件".$filename;
?>

第2种方法:

代码如下:

<?php
$host="localhost";
$user="root";
$password="";
$dbname="dbname";
backup_tables($host,$user,$password,$dbname);
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = &#39;*&#39;)
{
  $link = mysql_connect($host,$user,$pass);
  mysql_select_db($name,$link);
  //get all of the tables
  if($tables == &#39;*&#39;)
  {
    $tables = array();
    $result = mysql_query(&#39;SHOW TABLES&#39;);
    while($row = mysql_fetch_row($result))
    {
      $tables[] = $row[0];
    }
  }
  else
  {
    $tables = is_array($tables) ? $tables : explode(&#39;,&#39;,$tables);
  }
  $return = &#39;&#39;;
  //cycle through
  foreach($tables as $table)
  {
    $result = mysql_query(&#39;SELECT * FROM &#39;.$table);
    $num_fields = mysql_num_fields($result);    
    $return.= &#39;DROP TABLE &#39;.$table.&#39;;&#39;;
    $row2 = mysql_fetch_row(mysql_query(&#39;SHOW CREATE TABLE &#39;.$table));
    $return.= "\n\n".$row2[1].";\n\n";

    for ($i = 0; $i < $num_fields; $i++) 
    {
      while($row = mysql_fetch_row($result))
      {
        $return.= &#39;INSERT INTO &#39;.$table.&#39; VALUES(&#39;;
        for($j=0; $j<$num_fields; $j++) 
        {
          $row[$j] = addslashes($row[$j]);
          $row[$j] = ereg_replace("\n","\\n",$row[$j]);
          if (isset($row[$j])) { $return.= &#39;"&#39;.$row[$j].&#39;"&#39; ; } else { $return.= &#39;""&#39;; }
          if ($j<($num_fields-1)) { $return.= &#39;,&#39;; }
        }
        $return.= ");\n";
      }
    }
    $return.="\n\n\n";
  }

  //save file
  $handle = fopen(&#39;db-backup-&#39;.time().&#39;-&#39;.(md5(implode(&#39;,&#39;,$tables))).&#39;.sql&#39;,&#39;w+&#39;);
  fwrite($handle,$return);
  fclose($handle);
}
?>

【相关教程推荐】

1. php编程从入门到精通全套视频教程
2. php从入门到精通 
3. bootstrap教程

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