溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php怎么將mysql數(shù)據(jù)庫(kù)整庫(kù)導(dǎo)出生成sql文件

發(fā)布時(shí)間:2021-08-09 11:56:18 來(lái)源:億速云 閱讀:137 作者:chen 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“php怎么將mysql數(shù)據(jù)庫(kù)整庫(kù)導(dǎo)出生成sql文件”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“php怎么將mysql數(shù)據(jù)庫(kù)整庫(kù)導(dǎo)出生成sql文件”吧!



文件名: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數(shù)據(jù)庫(kù)整庫(kù)導(dǎo)出生成sql文件”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)php怎么將mysql數(shù)據(jù)庫(kù)整庫(kù)導(dǎo)出生成sql文件這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI