php將mysql數(shù)據(jù)庫(kù)整庫(kù)導(dǎo)出生成sql文件的具體實(shí)現(xiàn)
更新時(shí)間:2014年01月08日 16:06:09 作者:
下面是php將mysql數(shù)據(jù)庫(kù)整庫(kù)導(dǎo)出生成sql文件的詳細(xì)代碼,希望對(duì)大家在用php編程時(shí)備份數(shù)據(jù)有一定幫助
由網(wǎng)上搜到,有更改。
文件名:db_backup.php
源代碼如下:
<?php
ini_set("max_execution_time", "180");//避免數(shù)據(jù)量過(guò)大,導(dǎo)出不全的情況出現(xiàn)。
/*
程序功能:mysql數(shù)據(jù)庫(kù)備份功能
作者:唐小剛
說(shuō)明:
本程序主要是從mysqladmin中提取出來(lái),并作出一定的調(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ù)庫(kù)地址
$dbname="";//這里配置數(shù)據(jù)庫(kù)名
$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ù)庫(kù)主機(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;
}
/*
//原來(lái)別人的取得數(shù)據(jù)庫(kù)結(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;
}
?>
文件名:db_backup.php
源代碼如下:
復(fù)制代碼 代碼如下:
<?php
ini_set("max_execution_time", "180");//避免數(shù)據(jù)量過(guò)大,導(dǎo)出不全的情況出現(xiàn)。
/*
程序功能:mysql數(shù)據(jù)庫(kù)備份功能
作者:唐小剛
說(shuō)明:
本程序主要是從mysqladmin中提取出來(lái),并作出一定的調(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ù)庫(kù)地址
$dbname="";//這里配置數(shù)據(jù)庫(kù)名
$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ù)庫(kù)主機(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;
}
/*
//原來(lái)別人的取得數(shù)據(jù)庫(kù)結(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之Mysql常用SQL語(yǔ)句示例的深入分析
- PHP執(zhí)行批量mysql語(yǔ)句的解決方法
- 工作中常用的mysql語(yǔ)句分享 不用php也可以實(shí)現(xiàn)的效果
- php與mysql建立連接并執(zhí)行SQL語(yǔ)句的代碼
- php中轉(zhuǎn)義mysql語(yǔ)句的實(shí)現(xiàn)代碼
- php 備份數(shù)據(jù)庫(kù)代碼(生成word,excel,json,xml,sql)
- PHP備份數(shù)據(jù)庫(kù)生成SQL文件并下載的函數(shù)代碼
- PHP FOR MYSQL 代碼生成助手(根據(jù)Mysql里的字段自動(dòng)生成類文件的)
- php中比較簡(jiǎn)單的導(dǎo)入phpmyadmin生成的sql文件的方法
- php SQL之where語(yǔ)句生成器
- PHP+Mysql實(shí)現(xiàn)多關(guān)鍵字與多字段生成SQL語(yǔ)句的函數(shù)
相關(guān)文章
php 實(shí)現(xiàn)進(jìn)制相互轉(zhuǎn)換
最近的項(xiàng)目中需要用到進(jìn)制轉(zhuǎn)換,這個(gè)問(wèn)題在剛剛接觸計(jì)算機(jī)理論時(shí)候,還是很會(huì)的,好久不用,居然模糊了……2016-04-04
PHP實(shí)現(xiàn)上一篇下一篇的方法實(shí)例總結(jié)
這篇文章主要介紹了PHP實(shí)現(xiàn)上一篇下一篇的方法,結(jié)合實(shí)例形式總結(jié)分析了php獲取上一篇下一篇文章SQL操作的相關(guān)查詢技巧,需要的朋友可以參考下2016-09-09
學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)觀察者模式(Observer)
這篇文章主要介紹了php設(shè)計(jì)模式中的觀察者模式,使用php實(shí)現(xiàn)觀察者模式,感興趣的小伙伴們可以參考一下2015-12-12
redis查看連接數(shù)及php模擬并發(fā)創(chuàng)建redis連接的方法
下面小編就為大家?guī)?lái)一篇redis查看連接數(shù)及php模擬并發(fā)創(chuàng)建redis連接的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
php 隨機(jī)記錄mysql rand()造成CPU 100%的解決辦法
mysql數(shù)據(jù)庫(kù)有10幾萬(wàn)條數(shù)據(jù),使用rand()提取隨機(jī)10條記錄,導(dǎo)致服務(wù)器cpu占用居高不下直至死機(jī)~2010-05-05
PHP實(shí)現(xiàn)返回JSON和XML的類分享
這篇文章主要給大家分享了一個(gè)使用PHP實(shí)現(xiàn)返回JSON和XML的類,非常實(shí)用,希望大家能夠喜歡2015-01-01

