//无论客户端是否关闭浏览器,下面的代码都将得到执行。
ignore_user_abort(true);
set_time_limit(0);
//function write_txt(){
ini_set("max_execution_time", "180");
//避免数据量过大,导出不全的情况出现。
$host="127.0.0.1";
//数据库地址
$dbname="EQMIS";
//这里配置数据库名
$username="root";
//用户名
$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");
//备份数据
$i = 0;
$crlf="\r\n";
global $dbconn;
$dbconn = mysqli_connect($host,$username,$passw);
//数据库主机,用户名,密码
$db = mysqli_select_db($dbconn,$dbname);
$dbconn->query("SET NAMES 'utf8'");
mysqli_set_charset($dbconn, 'utf-8');
$tables = mysqli_query($dbconn,"SHOW TABLES FROM $dbname");
//$tables = mysqli_query($dbconn,$dbname);
$num_tables = mysqli_num_rows($tables);
print "-- filename=".$filename;
while($i < $num_tables) {
$table = mysqli_fetch_assoc($tables);
print $crlf;
echo get_table_structure($dbconn, $table, $crlf) . ";$crlf$crlf";
//echo get_table_def($dbname, $table, $crlf) . ";$crlf$crlf";
echo get_table_content($dbconn, $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 = mysqli_query($db,"SHOW CREATE TABLE $table");
$row=mysqli_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 = mysqli_query($db, "SELECT * FROM $table");
$i = 0;
while($row = mysqli_fetch_row($result)) {
$schema_insert = "INSERT INTO `$table` VALUES (";
for($j=0; $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;
}
//usleep适用于php5.0以上,定时
function do_cron(){
usleep(10000);
write_txt();
}
while(1){
do_cron();
}
?>
一键复制
编辑
Web IDE
原始数据
按行查看
历史