您当前的位置: 首页 > 网站编程 > PHP教程 > php将mysql数据库整库导出生成sql文件的详细代码

php将mysql数据库整库导出生成sql文件的详细代码

作者:不详 来源:网络 发布时间: 2014-08-11 08:45 点击:
下面是php将mysql数据库整库导出生成sql文件的详细代码,希望对大家在用php编程时备份数据有一定帮助 由网上搜到,有更改。 文件名:db_backup.php 源代码如下: 代码如下: ?php ini_set(max_execution_time, 180);//避免数据量过大,导出不全的情况出现。 /* 程序功能

php将mysql数据库整库导出生成sql文件的详细代码

  下面是php将mysql数据库整库导出生成sql文件的详细代码,希望对大家在用php编程时备份数据有一定帮助

  由网上搜到,有更改。

  文件名:db_backup.php

  源代码如下:

  代码如下:

  <?php

  ini_set("max_execution_time", "180");//避免数据量过大,导出不全的情况出现。

  /*

  程序功能:mysql数据库备份功能

  作者:唐小刚

  说明:

  本程序主要是从mysqladmin中提取出来,并作出一定的调整,希望对大家在用php编程时备份数据有一定帮助.

  如果不要备份结构:请屏掉这句:echo get_table_structure($dbname, $table, $crlf).";$crlf$crlf";

  如果不要备份内容:请屏掉这句:echo get_table_content($dbname, $table, $crlf);

  修改者:何锦盛

  修改时间:2009/11/7

  修改内容:新增函数get_table_structure,注释掉了函数get_table_def,目的是获得更丰富的建表时的细节(如:ENGINE=InnoDB AUTO_INCREMENT=80 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='商品信息变更信息')

  */

  $host="";//数据库地址

  $dbname="";//这里配置数据库名

  $username="";//用户名

  $passw="";//这里配置密码

  $filename=date("Y-m-d_H-i-s")."-".$dbname.".sql";

  header("Content-disposition:filename=".$filename);//所保存的文件名

  header("Content-type:application/octetstream");

  header("Pragma:no-cache");

  header("Expires:0");

  //备份数据

  $i = 0;

  $crlf="rn";

  global $dbconn;

  $dbconn = mysql_connect($host,$username,$passw]);//数据库主机,用户名,密码

  $db = mysql_select_db($dbname,$dbconn);

  mysql_query("SET NAMES 'utf8'");

  $tables =mysql_list_tables($dbname,$dbconn);

  $num_tables = @mysql_numrows($tables);

  print "-- filename=".$filename;

  while($i < $num_tables)

  {

  $table=mysql_tablename($tables,$i);

  print $crlf;

  echo get_table_structure($dbname, $table, $crlf).";$crlf$crlf";

  //echo get_table_def($dbname, $table, $crlf).";$crlf$crlf";

  echo get_table_content($dbname, $table, $crlf);

  $i++;

  }

  /*新增的获得详细表结构*/

  function get_table_structure($db,$table,$crlf)

  {

  global $drop;

  $schema_create = "";

  if(!empty($drop)){ $schema_create .= "DROP TABLE IF EXISTS `$table`;$crlf";}

  $result =mysql_db_query($db, "SHOW CREATE TABLE $table");

  $row=mysql_fetch_array($result);

  $schema_create .= $crlf."-- ".$row[0].$crlf;

  $schema_create .= $row[1].$crlf;

  Return $schema_create;

  }

  /*

  //原来别人的取得数据库结构,但不完整

  function get_table_def($db,$table,$crlf)

  {

  global $drop;

  $schema_create = "";

  if(!empty($drop))

  $schema_create .= "DROP TABLE IF EXISTS `$table`;$crlf";

  $schema_create .= "CREATE TABLE `$table` ($crlf";

  $result = mysql_db_query($db, "SHOW full FIELDS FROM $table");

  while($row = mysql_fetch_array($result))

  {

  $schema_create .= " `$row[Field]` $row[Type]";

  if(isset($row["Default"]) && (!empty($row["Default"]) || $row["Default"] == "0"))

  $schema_create .= " DEFAULT '$row[Default]'";

  if($row["Null"] != "YES")

  $schema_create .= " NOT NULL";

  if($row["Extra"] != "")

  $schema_create .= " $row[Extra]";

  if($row["Comment"] != "")

  $schema_create .= " Comment '$row[Comment]'";

  $schema_create .= ",$crlf";

  }

  $schema_create = ereg_replace(",".$crlf."$", "", $schema_create);

  $result = mysql_db_query($db, "SHOW KEYS FROM $table");

  while($row = mysql_fetch_array($result))

  {

  $kname=$row['Key_name'];

  if(($kname != "PRIMARY") && ($row['Non_unique'] == 0))

  $kname="UNIQUE|$kname";

  if(!isset($index[$kname]))

  $index[$kname] = array();

  $index[$kname][] = $row['Column_name'];

  }

  while(list($x,$columns) = @each($index))

  {

  $schema_create .= ",$crlf";

  if($x == "PRIMARY")

  $schema_create .= " PRIMARY KEY (".implode($columns,", ") . ")";

  elseif (substr($x,0,6) == "UNIQUE")

  $schema_create .= " UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")";

  else

  $schema_create .= " KEY $x (" . implode($columns, ", ") . ")";

  }

  $schema_create .= "$crlf)";

  return (stripslashes($schema_create));

  }

  */

  //获得表内容

  function get_table_content($db, $table, $crlf)

  {

  $schema_create = "";

  $temp = "";

  $result = mysql_db_query($db, "SELECT * FROM $table");

  $i = 0;

  while($row = mysql_fetch_row($result))

  {

  $schema_insert = "INSERT INTO `$table` VALUES (";

  for($j=0; $j<mysql_num_fields($result);$j++)

  {

  if(!isset($row[$j]))

  $schema_insert .= " NULL,";

  elseif($row[$j] != "")

  $schema_insert .= " '".addslashes($row[$j])."',";

  else

  $schema_insert .= " '',";

  }

  $schema_insert = ereg_replace(",$", "",$schema_insert);

  $schema_insert .= ");$crlf";

  $temp = $temp.$schema_insert ;

  $i++;

  }

  return $temp;

  }

  ?>
分享到:
本文"php将mysql数据库整库导出生成sql文件的详细代码"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4