加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

oa系统信用盘出租-php定时备份数据库数据,导出sql文件

发布时间:2020-12-13 21:30:21 所属栏目:PHP教程 来源:网络整理
导读:?php //无论客户端是否关闭浏览器,下面的代码都将得到执行。 //{OA信用盘架设q319.135.503.1} ignore_user_abort(true);set_time_limit(0); //function write_txt(){ ini_set("max_execution_time","180");//避免数据量过大,导出不全的情况出现。 $host="1
<?php
//无论客户端是否关闭浏览器,下面的代码都将得到执行。
//{OA信用盘架设q<319.135.503.1>}
ignore_user_abort(true);set_time_limit(0);
//function write_txt(){
ini_set("max_execution_time","180");//避免数据量过大,导出不全的情况出现。

$host="127.0.0.1";//数据库地址
$dbname="apiadmin";//这里配置数据库名
$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="rn";
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);
$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,$crlf).";$crlf$crlf";
echo get_table_content($dbname,$crlf);
$i++;
}
//}
function get_table_structure($db,$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;
}

//获得表内容
function get_table_content($db,$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;
}

//usleep适用于php5.0以上,定时
function do_cron(){usleep(1);write_txt();}

while(1){do_cron();}?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读