国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php將mysql數據庫整庫導出生成sql文件的具體實現

2024-05-04 23:20:20
字體:
來源:轉載
供稿:網友
由網上搜到,有更改。

文件名: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;
}
?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大渡口区| 梁平县| 浙江省| 康平县| 韶山市| 大方县| 淳化县| 柘荣县| 密云县| 确山县| 沙湾县| 伊春市| 利津县| 澄迈县| 玛纳斯县| 南城县| 云安县| 阳谷县| 双辽市| 麦盖提县| 会宁县| 康乐县| 盖州市| 西华县| 永兴县| 尉犁县| 乾安县| 凤凰县| 昌吉市| 岳普湖县| 广西| 普兰店市| 平阳县| 葫芦岛市| 榆中县| 东阳市| 全椒县| 岑溪市| 开江县| 云阳县| 洪湖市|