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í)計(jì)算器功能示例
這篇文章主要介紹了PHP基于堆棧實(shí)現(xiàn)的高級(jí)計(jì)算器功能,涉及php堆棧的定義及使用堆棧進(jìn)行數(shù)值運(yùn)算的相關(guān)操作技巧,需要的朋友可以參考下2017-09-09

php的無(wú)刷新操作實(shí)現(xiàn)方法分析
這篇文章主要介紹了php的無(wú)刷新操作實(shí)現(xiàn)方法,結(jié)合實(shí)例形式總結(jié)分析了PHP無(wú)刷新操作常見(jiàn)原理、實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下
2020-02-02 
有關(guān) PHP 和 MySQL 時(shí)區(qū)的一點(diǎn)總結(jié)
由于暫時(shí)使用國(guó)外的空間,在我發(fā)布 Blog 的時(shí)候發(fā)現(xiàn)時(shí)間總是不對(duì)。依據(jù)我以前編寫(xiě)程序的經(jīng)驗(yàn),這是時(shí)區(qū)的問(wèn)題。這個(gè)問(wèn)題解決起來(lái)并不難,寫(xiě)下我的解決途徑以便日后參考。
2008-03-03