php将mysql数据库整库导出生成sql文件php
php将mysql数据库整库导出生成sql文件...
<?php
ini_set("max_execution_time", "180");//避免数据量过大,导出不全的情况出现。
$host="localhost";//数据库地址
$dbname="db";//这里配置数据库名
$username="root";//用户名
$passw="root";//这里配置密码
$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);
$tables =mysql_query("SHOW TABLES FROM $dbname");
$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_query("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_content($db, $table, $crlf){
$schema_create = "";
$temp = "";
$result = mysql_query("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 = preg_replace("/,$/","",$schema_insert);
$schema_insert .= ");$crlf";
$temp = $temp.$schema_insert ;
$i++;
}
return $temp;
}
?>
ini_set("max_execution_time", "180");//避免数据量过大,导出不全的情况出现。
$host="localhost";//数据库地址
$dbname="db";//这里配置数据库名
$username="root";//用户名
$passw="root";//这里配置密码
$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);
$tables =mysql_query("SHOW TABLES FROM $dbname");
$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_query("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_content($db, $table, $crlf){
$schema_create = "";
$temp = "";
$result = mysql_query("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 = preg_replace("/,$/","",$schema_insert);
$schema_insert .= ");$crlf";
$temp = $temp.$schema_insert ;
$i++;
}
return $temp;
}
?>
上一篇:php数组模糊查询
下一篇:php文件路径获取文件名
最新评论
热门推荐
我要评论