欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php将mysql数据库整库导出生成sql文件的具体实现

程序员文章站 2023-11-20 21:10:16
由网上搜到,有更改。 文件名:db_backup.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="\r\n";
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;
}
?>