当前位置:Gxlcms > PHP教程 > php将mysql数据库整库导出生成sql文件的具体实现_PHP教程

php将mysql数据库整库导出生成sql文件的具体实现_PHP教程

时间:2021-07-01 10:21:17 帮助过:3人阅读

由网上搜到,有更改。

文件名:db_backup.php

源代码如下:
代码如下:
  1. <br><!--?php <BR-->ini_set("max_execution_time", "180");//避免数据量过大,导出不全的情况出现。 <br><br>/* <br><br>程序功能:mysql数据库备份功能 <br>作者:唐小刚 <br>说明: <br>本程序主要是从mysqladmin中提取出来,并作出一定的调整,希望对大家在用php编程时备份数据有一定帮助. <br>如果不要备份结构:请屏掉这句:echo get_table_structure($dbname, $table, $crlf).";$crlf$crlf"; <br>如果不要备份内容:请屏掉这句:echo get_table_content($dbname, $table, $crlf); <br><br>修改者:何锦盛 <br>修改时间:2009/11/7 <br>修改内容:新增函数get_table_structure,注释掉了函数get_table_def,目的是获得更丰富的建表时的细节(如:ENGINE=InnoDB AUTO_INCREMENT=80 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='商品信息变更信息') <br>*/ <br><br>$host="";//数据库地址 <br><br>$dbname="";//这里配置数据库名 <br><br>$username="";//用户名 <br><br>$passw="";//这里配置密码 <br><br>$filename=date("Y-m-d_H-i-s")."-".$dbname.".sql"; <br>header("Content-disposition:filename=".$filename);//所保存的文件名 <br>header("Content-type:application/octetstream"); <br>header("Pragma:no-cache"); <br>header("Expires:0"); <br><br>//备份数据 <br>$i = 0; <br>$crlf="\r\n"; <br>global $dbconn; <br>$dbconn = mysql_connect($host,$username,$passw]);//数据库主机,用户名,密码 <br>$db = mysql_select_db($dbname,$dbconn); <br>mysql_query("SET NAMES 'utf8'"); <br>$tables =mysql_list_tables($dbname,$dbconn); <br>$num_tables = @mysql_numrows($tables); <br>print "-- filename=".$filename; <br>while($i < $num_tables) <br>{ <br>$table=mysql_tablename($tables,$i); <br>print $crlf; <br>echo get_table_structure($dbname, $table, $crlf).";$crlf$crlf"; <br>//echo get_table_def($dbname, $table, $crlf).";$crlf$crlf"; <br>echo get_table_content($dbname, $table, $crlf); <br>$i++; <br>} <br><br>/*新增的获得详细表结构*/ <br>function get_table_structure($db,$table,$crlf) <br>{ <br>global $drop; <br><br>$schema_create = ""; <br>if(!empty($drop)){ $schema_create .= "DROP TABLE IF EXISTS `$table`;$crlf";} <br>$result =mysql_db_query($db, "SHOW CREATE TABLE $table"); <br>$row=mysql_fetch_array($result); <br>$schema_create .= $crlf."-- ".$row[0].$crlf; <br>$schema_create .= $row[1].$crlf; <br>Return $schema_create; <br>} <br><br>/* <br>//原来别人的取得数据库结构,但不完整 <br>function get_table_def($db,$table,$crlf) <br>{ <br>global $drop; <br><br>$schema_create = ""; <br>if(!empty($drop)) <br>$schema_create .= "DROP TABLE IF EXISTS `$table`;$crlf"; <br><br>$schema_create .= "CREATE TABLE `$table` ($crlf"; <br>$result = mysql_db_query($db, "SHOW full FIELDS FROM $table"); <br>while($row = mysql_fetch_array($result)) <br>{ <br>$schema_create .= " `$row[Field]` $row[Type]"; <br><br>if(isset($row["Default"]) && (!empty($row["Default"]) || $row["Default"] == "0")) <br>$schema_create .= " DEFAULT '$row[Default]'"; <br>if($row["Null"] != "YES") <br>$schema_create .= " NOT NULL"; <br>if($row["Extra"] != "") <br>$schema_create .= " $row[Extra]"; <br>if($row["Comment"] != "") <br>$schema_create .= " Comment '$row[Comment]'"; <br>$schema_create .= ",$crlf"; <br>} <br>$schema_create = ereg_replace(",".$crlf."$", "", $schema_create); <br>$result = mysql_db_query($db, "SHOW KEYS FROM $table"); <br>while($row = mysql_fetch_array($result)) <br>{ <br>$kname=$row['Key_name']; <br>if(($kname != "PRIMARY") && ($row['Non_unique'] == 0)) <br>$kname="UNIQUE|$kname"; <br>if(!isset($index[$kname])) <br>$index[$kname] = array(); <br>$index[$kname][] = $row['Column_name']; <br>} <br><br>while(list($x,$columns) = @each($index)) <br>{ <br>$schema_create .= ",$crlf"; <br>if($x == "PRIMARY") <br>$schema_create .= " PRIMARY KEY (".implode($columns,", ") . ")"; <br>elseif (substr($x,0,6) == "UNIQUE") <br>$schema_create .= " UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")"; <br>else <br>$schema_create .= " KEY $x (" . implode($columns, ", ") . ")"; <br>} <br><br>$schema_create .= "$crlf)"; <br>return (stripslashes($schema_create)); <br>} <br>*/ <br><br>//获得表内容 <br>function get_table_content($db, $table, $crlf) <br>{ <br>$schema_create = ""; <br>$temp = ""; <br>$result = mysql_db_query($db, "SELECT * FROM $table"); <br>$i = 0; <br>while($row = mysql_fetch_row($result)) <br>{ <br>$schema_insert = "INSERT INTO `$table` VALUES ("; <br>for($j=0; $j<mysql_num_fields($result);$j++) <br="">{ <br>if(!isset($row[$j])) <br>$schema_insert .= " NULL,"; <br>elseif($row[$j] != "") <br>$schema_insert .= " '".addslashes($row[$j])."',"; <br>else <br>$schema_insert .= " '',"; <br>} <br>$schema_insert = ereg_replace(",$", "",$schema_insert); <br>$schema_insert .= ");$crlf"; <br>$temp = $temp.$schema_insert ; <br>$i++; <br>} <br>return $temp; <br>} <br>?> <br>
  2. <p></p>
  3. <p align="left"><span id="url" itemprop="url">http://www.bkjia.com/PHPjc/676878.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/676878.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">由网上搜到,有更改。 文件名:db_backup.php 源代码如下: 代码如下:</span></p><pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li>?php ini_set("max_execution_time", "180");//避免数据量过大,导出不全...<p></p></li><li> </li></ol></pre></mysql_num_fields($result);$j++)>

人气教程排行