程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 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="\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;
}
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved