文件名:db_backup.php

源代碼如下: 復(fù)制代碼 代碼如下: <?php ini_set("max_execution_time", "180");//避免數(shù)據(jù)量過大,導(dǎo)出不全的情況出現(xiàn)。

/*

程序功能:mysql數(shù)據(jù)庫備份功能 作者:唐小剛 " /> 中文字幕高清在线播放,国产一区二区三区在线看麻豆,在线免费日韩片

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php將mysql數(shù)據(jù)庫整庫導(dǎo)出生成sql文件的具體實(shí)現(xiàn)

由網(wǎng)上搜到,有更改。

文件名:db_backup.php

源代碼如下:
復(fù)制代碼 代碼如下:
<?php
ini_set("max_execution_time", "180");//避免數(shù)據(jù)量過大,導(dǎo)出不全的情況出現(xiàn)。

/*

程序功能:mysql數(shù)據(jù)庫備份功能
作者:唐小剛
說明:
本程序主要是從mysqladmin中提取出來,并作出一定的調(diào)整,希望對(duì)大家在用php編程時(shí)備份數(shù)據(jù)有一定幫助.
如果不要備份結(jié)構(gòu):請(qǐng)屏掉這句:echo get_table_structure($dbname, $table, $crlf).";$crlf$crlf";
如果不要備份內(nèi)容:請(qǐng)屏掉這句:echo get_table_content($dbname, $table, $crlf);

修改者:何錦盛
修改時(shí)間:2009/11/7
修改內(nèi)容:新增函數(shù)get_table_structure,注釋掉了函數(shù)get_table_def,目的是獲得更豐富的建表時(shí)的細(xì)節(jié)(如:ENGINE=InnoDB AUTO_INCREMENT=80 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='商品信息變更信息')
*/

$host="";//數(shù)據(jù)庫地址

$dbname="";//這里配置數(shù)據(jù)庫名

$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");

//備份數(shù)據(jù)
$i = 0;
$crlf="/r/n";
global $dbconn;
$dbconn = mysql_connect($host,$username,$passw]);//數(shù)據(jù)庫主機(jī),用戶名,密碼
$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++;
}

/*新增的獲得詳細(xì)表結(jié)構(gòu)*/
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;
}

/*
//原來別人的取得數(shù)據(jù)庫結(jié)構(gòu),但不完整
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));
}
*/

//獲得表內(nèi)容
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技術(shù)php將mysql數(shù)據(jù)庫整庫導(dǎo)出生成sql文件的具體實(shí)現(xiàn),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 蚌埠市| 元氏县| 静乐县| 腾冲县| 永宁县| 洛川县| 涿州市| 石狮市| 曲阜市| 陇川县| 仪征市| 云浮市| 靖边县| 永川市| 宁海县| 汨罗市| 南漳县| 江阴市| 大化| 大连市| 华蓥市| 太谷县| 长沙县| 松江区| 右玉县| 广宗县| 沧州市| 澜沧| 定州市| 泸水县| 九龙县| 平邑县| 东乡| 会昌县| 白水县| 旅游| 屏南县| 左贡县| 珲春市| 册亨县| 芮城县|