php database backup and restore
Jul 11, 2016 pm 08:00 PM1. mydb.php <span style="color: #008000;">//</span><span style="color: #008000;">DB類</span> 2. backup.php <span style="color: #008000;">//</span><span style="color: #008000;">備份腳本</span> 3. restore.php <span style="color: #008000;">//</span><span style="color: #008000;">還原腳本</span> <span style="color: #000000;"> mydb</span>.<span style="color: #000000;">php </span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> db{ </span><span style="color: #0000ff;">var</span> <span style="color: #800080;">$linkid</span><span style="color: #000000;">; </span><span style="color: #0000ff;">var</span> <span style="color: #800080;">$sqlid</span><span style="color: #000000;">; </span><span style="color: #0000ff;">var</span> <span style="color: #800080;">$record</span><span style="color: #000000;">; </span><span style="color: #0000ff;">function</span> db(<span style="color: #800080;">$host</span>="",<span style="color: #800080;">$username</span>="",<span style="color: #800080;">$password</span>="",<span style="color: #800080;">$database</span>=""<span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$this</span>->linkid) @<span style="color: #800080;">$this</span>->linkid = <span style="color: #008080;">mysql_connect</span>(<span style="color: #800080;">$host</span>, <span style="color: #800080;">$username</span>, <span style="color: #800080;">$password</span>) or <span style="color: #0000ff;">die</span>("連接服務(wù)器失敗."<span style="color: #000000;">); @</span><span style="color: #008080;">mysql_select_db</span>(<span style="color: #800080;">$database</span>,<span style="color: #800080;">$this</span>->linkid) or <span style="color: #0000ff;">die</span>("無法打開數(shù)據(jù)庫"<span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">linkid;} </span><span style="color: #0000ff;">function</span> query(<span style="color: #800080;">$sql</span><span style="color: #000000;">) {</span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->sqlid=<span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span>,<span style="color: #800080;">$this</span>->linkid)) <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">sqlid; </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #800080;">$this</span>->err_report(<span style="color: #800080;">$sql</span>,<span style="color: #008080;">mysql_error</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;} } </span><span style="color: #0000ff;">function</span> nr(<span style="color: #800080;">$sql_id</span>=""<span style="color: #000000;">) {</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$sql_id</span>) <span style="color: #800080;">$sql_id</span>=<span style="color: #800080;">$this</span>-><span style="color: #000000;">sqlid; </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">mysql_num_rows</span>(<span style="color: #800080;">$sql_id</span><span style="color: #000000;">);} </span><span style="color: #0000ff;">function</span> nf(<span style="color: #800080;">$sql_id</span>=""<span style="color: #000000;">) {</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$sql_id</span>) <span style="color: #800080;">$sql_id</span>=<span style="color: #800080;">$this</span>-><span style="color: #000000;">sqlid; </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">mysql_num_fields</span>(<span style="color: #800080;">$sql_id</span><span style="color: #000000;">);} </span><span style="color: #0000ff;">function</span> nextrecord(<span style="color: #800080;">$sql_id</span>=""<span style="color: #000000;">) {</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$sql_id</span>) <span style="color: #800080;">$sql_id</span>=<span style="color: #800080;">$this</span>-><span style="color: #000000;">sqlid; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->record=<span style="color: #008080;">mysql_fetch_array</span>(<span style="color: #800080;">$sql_id</span>)) <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">record; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span> f(<span style="color: #800080;">$name</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->record[<span style="color: #800080;">$name</span>]) <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->record[<span style="color: #800080;">$name</span><span style="color: #000000;">]; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span> close() {<span style="color: #008080;">mysql_close</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">linkid);} </span><span style="color: #0000ff;">function</span> lock(<span style="color: #800080;">$tblname</span>,<span style="color: #800080;">$op</span>="WRITE"<span style="color: #000000;">) {</span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">mysql_query</span>("lock tables ".<span style="color: #800080;">$tblname</span>." ".<span style="color: #800080;">$op</span>)) <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span>; <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;} </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> unlock() {</span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">mysql_query</span>("unlock tables")) <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span>; <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;} </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> ar() { </span><span style="color: #0000ff;">return</span> @<span style="color: #008080;">mysql_affected_rows</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">linkid); } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> i_id() { </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">mysql_insert_id</span><span style="color: #000000;">(); } </span><span style="color: #0000ff;">function</span> err_report(<span style="color: #800080;">$sql</span>,<span style="color: #800080;">$err</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "Mysql查詢錯誤<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "查詢語句:".<span style="color: #800080;">$sql</span>."<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "錯誤信息:".<span style="color: #800080;">$err</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">***************************************類結(jié)束**************************</span><span style="color: #008000;">*/</span><span style="color: #000000;"> }</span>?><span style="color: #000000;"> backup</span>.<span style="color: #000000;">php </span>
<span style="color: #0000ff;">error_reporting(E_ALL & ~ E_NOTICE);</span>
<span style="color: #0000ff;">global</span> <span style="color: #800080;">$mysqlhost</span>, <span style="color: #800080;">$mysqluser</span>, <span style="color: #800080;">$mysqlpwd</span>, <span style="color: #800080;">$mysqldb</span><span style="color: #000000;">; </span><span style="color: #800080;">$mysqlhost</span>="localhost"; <span style="color: #008000;">//</span><span style="color: #008000;">host name</span> <span style="color: #800080;">$mysqluser</span>="root"; <span style="color: #008000;">//</span><span style="color: #008000;">login name</span> <span style="color: #800080;">$mysqlpwd</span>=""; <span style="color: #008000;">//</span><span style="color: #008000;">password</span> <span style="color: #800080;">$mysqldb</span>=""; <span style="color: #008000;">//</span><span style="color: #008000;">name of database</span> <span style="color: #0000ff;">include</span>("mydb.php"<span style="color: #000000;">); </span><span style="color: #800080;">$d</span>=<span style="color: #0000ff;">new</span> db(<span style="color: #800080;">$mysqlhost</span>,<span style="color: #800080;">$mysqluser</span>,<span style="color: #800080;">$mysqlpwd</span>,<span style="color: #800080;">$mysqldb</span><span style="color: #000000;">); </span><span style="color: #008000;">/*</span><span style="color: #008000;">--------------界面--------------</span><span style="color: #008000;">*/</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$_POST</span>['act']){<span style="color: #008000;">/*</span><span style="color: #008000;">----------------------</span><span style="color: #008000;">*/</span> <span style="color: #800080;">$msgs</span>[]="服務(wù)器備份目錄為backup"<span style="color: #000000;">; </span><span style="color: #800080;">$msgs</span>[]="對于較大的數(shù)據(jù)表,強(qiáng)烈建議使用分卷備份"<span style="color: #000000;">; </span><span style="color: #800080;">$msgs</span>[]="只有選擇備份到服務(wù)器,才能使用分卷備份功能"<span style="color: #000000;">; show_msg(</span><span style="color: #800080;">$msgs</span><span style="color: #000000;">); </span>?>/*-------------界面結(jié)束-------------*/}/*---------------------------------*/ /*----*/else{/*--------------主程序-----------------------------------------*/ if($_POST['weizhi']=="localpc"&&$_POST['fenjuan']=='yes') {$msgs[]="只有選擇備份到服務(wù)器,才能使用分卷備份功能"; show_msg($msgs); pageend();} if($_POST['fenjuan']=="yes"&&!$_POST['filesize']) {$msgs[]="您選擇了分卷備份功能,但未填寫分卷文件大小"; show_msg($msgs); pageend();} if($_POST['weizhi']=="server"&&!writeable("./backup")) {$msgs[]="備份文件存放目錄'./backup'不可寫,請修改目錄屬性"; show_msg($msgs); pageend();} /*----------備份全部表-------------*/if($_POST['bfzl']=="quanbubiao"){/*----*/ /*----不分卷*/if(!$_POST['fenjuan']){/*--------------------------------*/ if(!$tables=$d->query("show table status from $mysqldb")) {$msgs[]="讀數(shù)據(jù)庫結(jié)構(gòu)錯誤"; show_msg($msgs); pageend();} $sql=""; while($d->nextrecord($tables)) { $table=$d->f("Name"); $sql.=make_header($table); $d->query("select * from $table"); $num_fields=$d->nf(); while($d->nextrecord()) {$sql.=make_record($table,$num_fields);} } $filename=date("Ymd",time())."_all.sql"; if($_POST['weizhi']=="localpc") down_file($sql,$filename); elseif($_POST['weizhi']=="server") {if(write_file($sql,$filename)) $msgs[]="全部數(shù)據(jù)表數(shù)據(jù)備份完成,生成備份文件'./backup/$filename'"; else $msgs[]="備份全部數(shù)據(jù)表失敗"; show_msg($msgs); pageend(); } /*-----------------不要卷結(jié)束*/}/*-----------------------*/ /*-----------------分卷*/else{/*-------------------------*/ if(!$_POST['filesize']) {$msgs[]="請?zhí)顚憘浞菸募志泶笮?; show_msg($msgs);pageend();} if(!$tables=$d->query("show table status from $mysqldb")) {$msgs[]="讀數(shù)據(jù)庫結(jié)構(gòu)錯誤"; show_msg($msgs); pageend();} $sql=""; $p=1; $filename=date("Ymd",time())."_all"; while($d->nextrecord($tables)) { $table=$d->f("Name"); $sql.=make_header($table); $d->query("select * from $table"); $num_fields=$d->nf(); while($d->nextrecord()) {$sql.=make_record($table,$num_fields); if(strlen($sql)>=$_POST['filesize']*1000){ $filename.=("_v".$p.".sql"); if(write_file($sql,$filename)) $msgs[]="全部數(shù)據(jù)表-卷-".$p."-數(shù)據(jù)備份完成,生成備份文件'./backup/$filename'"; else $msgs[]="備份表-".$_POST['tablename']."-失敗"; $p++; $filename=date("Ymd",time())."_all"; $sql="";} } } if($sql!=""){$filename.=("_v".$p.".sql"); if(write_file($sql,$filename)) $msgs[]="全部數(shù)據(jù)表-卷-".$p."-數(shù)據(jù)備份完成,生成備份文件'./backup/$filename'";} show_msg($msgs); /*---------------------分卷結(jié)束*/}/*--------------------------------------*/ /*--------備份全部表結(jié)束*/}/*---------------------------------------------*/ /*--------備份單表------*/elseif($_POST['bfzl']=="danbiao"){/*------------*/ if(!$_POST['tablename']) {$msgs[]="請選擇要備份的數(shù)據(jù)表"; show_msg($msgs); pageend();} /*--------不分卷*/if(!$_POST['fenjuan']){/*-------------------------------*/ $sql=make_header($_POST['tablename']); $d->query("select * from ".$_POST['tablename']); $num_fields=$d->nf(); while($d->nextrecord()) {$sql.=make_record($_POST['tablename'],$num_fields);} $filename=date("Ymd",time())."_".$_POST['tablename'].".sql"; if($_POST['weizhi']=="localpc") down_file($sql,$filename); elseif($_POST['weizhi']=="server") {if(write_file($sql,$filename)) $msgs[]="表-".$_POST['tablename']."-數(shù)據(jù)備份完成,生成備份文件'./backup/$filename'"; else $msgs[]="備份表-".$_POST['tablename']."-失敗"; show_msg($msgs); pageend(); } /*----------------不要卷結(jié)束*/}/*------------------------------------*/ /*----------------分卷*/else{/*--------------------------------------*/ if(!$_POST['filesize']) {$msgs[]="請?zhí)顚憘浞菸募志泶笮?; show_msg($msgs);pageend();} $sql=make_header($_POST['tablename']); $p=1; $filename=date("Ymd",time())."_".$_POST['tablename']; $d->query("select * from ".$_POST['tablename']); $num_fields=$d->nf(); while ($d->nextrecord()) { $sql.=make_record($_POST['tablename'],$num_fields); if(strlen($sql)>=$_POST['filesize']*1000){ $filename.=("_v".$p.".sql"); if(write_file($sql,$filename)) $msgs[]="表-".$_POST['tablename']."-卷-".$p."-數(shù)據(jù)備份完成,生成備份文件'./backup/$filename'"; else $msgs[]="備份表-".$_POST['tablename']."-失敗"; $p++; $filename=date("Ymd",time())."_".$_POST['tablename']; $sql="";} } if($sql!=""){$filename.=("_v".$p.".sql"); if(write_file($sql,$filename)) $msgs[]="表-".$_POST['tablename']."-卷-".$p."-數(shù)據(jù)備份完成,生成備份文件'./backup/$filename'";} show_msg($msgs); /*----------分卷結(jié)束*/}/*--------------------------------------------------*/ /*----------備份單表結(jié)束*/}/*----------------------------------------------*/ /*---*/}/*-------------主程序結(jié)束------------------------------------------*/ function write_file($sql,$filename) { $re=true; if(!@$fp=fopen("./backup/".$filename,"w+")) {$re=false; echo "failed to open target file";} if(!@fwrite($fp,$sql)) {$re=false; echo "failed to write file";} if(!@fclose($fp)) {$re=false; echo "failed to close target file";} return $re; } function down_file($sql,$filename) { ob_end_clean(); header("Content-Encoding: none"); header("Content-Type: ".(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ? 'application/octetstream' : 'application/octet-stream')); header("Content-Disposition: ".(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ? 'inline; ' : 'attachment; ')."filename=".$filename); header("Content-Length: ".strlen($sql)); header("Pragma: no-cache"); header("Expires: 0"); echo $sql; $e=ob_get_contents(); ob_end_clean(); } function writeable($dir) { if(!is_dir($dir)) { @mkdir($dir, 0777); } if(is_dir($dir)) { if($fp = @fopen("$dir/test.test", 'w')) { @fclose($fp); @unlink("$dir/test.test"); $writeable = 1; } else { $writeable = 0; } } return $writeable; } function make_header($table) {global $d; $sql="DROP TABLE IF EXISTS ".$table."\n"; $d->query("show create table ".$table); $d->nextrecord(); $tmp=preg_replace("/\n/","",$d->f("Create Table")); $sql.=$tmp."\n"; return $sql; } function make_record($table,$num_fields) {global $d; $comma=""; $sql .= "INSERT INTO ".$table." VALUES("; for($i = 0; $i $num_fields; $i++) {$sql .= ($comma."'".mysql_escape_string($d->record[$i])."'"); $comma = ",";} $sql .= ")\n"; return $sql; } function show_msg($msgs) { $title="提示:"; echo "
".$title." |
|
error_reporting(E_ALL & ~ E_NOTICE);
<span style="color: #008080;">session_start</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">global</span> <span style="color: #800080;">$mysqlhost</span>, <span style="color: #800080;">$mysqluser</span>, <span style="color: #800080;">$mysqlpwd</span>, <span style="color: #800080;">$mysqldb</span><span style="color: #000000;">; </span><span style="color: #800080;">$mysqlhost</span>="localhost"; <span style="color: #008000;">//</span><span style="color: #008000;">host name</span> <span style="color: #800080;">$mysqluser</span>="root"; <span style="color: #008000;">//</span><span style="color: #008000;">login name</span> <span style="color: #800080;">$mysqlpwd</span>=""; <span style="color: #008000;">//</span><span style="color: #008000;">password</span> <span style="color: #800080;">$mysqldb</span>=""; <span style="color: #008000;">//</span><span style="color: #008000;">name of database</span> <span style="color: #0000ff;">include</span>("mydb.php"<span style="color: #000000;">); </span><span style="color: #800080;">$d</span>=<span style="color: #0000ff;">new</span> db(<span style="color: #800080;">$mysqlhost</span>,<span style="color: #800080;">$mysqluser</span>,<span style="color: #800080;">$mysqlpwd</span>,<span style="color: #800080;">$mysqldb</span><span style="color: #000000;">); </span><span style="color: #008000;">/*</span><span style="color: #008000;">*****界面</span><span style="color: #008000;">*/</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$_POST</span>['act']&&!<span style="color: #800080;">$_SESSION</span>['data_file']){<span style="color: #008000;">/*</span><span style="color: #008000;">********************</span><span style="color: #008000;">*/</span> <span style="color: #800080;">$msgs</span>[]="本功能在恢復(fù)備份數(shù)據(jù)的同時,將全部覆蓋原有數(shù)據(jù),請確定是否需要恢復(fù),以免造成數(shù)據(jù)損失"<span style="color: #000000;">; </span><span style="color: #800080;">$msgs</span>[]="數(shù)據(jù)恢復(fù)功能只能恢復(fù)由dShop導(dǎo)出的數(shù)據(jù)文件,其他軟件導(dǎo)出格式可能無法識別"<span style="color: #000000;">; </span><span style="color: #800080;">$msgs</span>[]="從本地恢復(fù)數(shù)據(jù)需要服務(wù)器支持文件上傳并保證數(shù)據(jù)尺寸小于允許上傳的上限,否則只能使用從服務(wù)器恢復(fù)"<span style="color: #000000;">; </span><span style="color: #800080;">$msgs</span>[]="如果您使用了分卷備份,只需手工導(dǎo)入文件卷1,其他數(shù)據(jù)文件會由系統(tǒng)自動導(dǎo)入"<span style="color: #000000;">; show_msg(</span><span style="color: #800080;">$msgs</span><span style="color: #000000;">); </span>?>/**************************界面結(jié)束*/}/*************************************/ /****************************主程序*/if($_POST['act']=="恢復(fù)"){/**************/ /***************服務(wù)器恢復(fù)*/if($_POST['restorefrom']=="server"){/**************/ if(!$_POST['serverfile']) {$msgs[]="您選擇從服務(wù)器文件恢復(fù)備份,但沒有指定備份文件"; show_msg($msgs); pageend(); } if(!preg_match("/_v[0-9]+/",$_POST['serverfile'])) {$filename="./backup/".$_POST['serverfile']; if(import($filename)) $msgs[]="備份文件".$_POST['serverfile']."成功導(dǎo)入數(shù)據(jù)庫"; else $msgs[]="備份文件".$_POST['serverfile']."導(dǎo)入失敗"; show_msg($msgs); pageend(); } else { $filename="./backup/".$_POST['serverfile']; if(import($filename)) $msgs[]="備份文件".$_POST['serverfile']."成功導(dǎo)入數(shù)據(jù)庫"; else {$msgs[]="備份文件".$_POST['serverfile']."導(dǎo)入失敗";show_msg($msgs);pageend();} $voltmp=explode("_v",$_POST['serverfile']); $volname=$voltmp[0]; $volnum=explode(".sq",$voltmp[1]); $volnum=intval($volnum[0])+1; $tmpfile=$volname."_v".$volnum.".sql"; if(file_exists("./backup/".$tmpfile)) { $msgs[]="程序?qū)⒃?秒鐘后自動開始導(dǎo)入此分卷備份的下一部份:文件".$tmpfile.",請勿手動中止程序的運行,以免數(shù)據(jù)庫結(jié)構(gòu)受損"; $_SESSION['data_file']=$tmpfile; show_msg($msgs); sleep(3); echo ""; } else { $msgs[]="此分卷備份全部導(dǎo)入成功"; show_msg($msgs); } } /**************服務(wù)器恢復(fù)結(jié)束*/}/********************************************/ /*****************本地恢復(fù)*/if($_POST['restorefrom']=="localpc"){/**************/ switch ($_FILES['myfile']['error']) { case 1: case 2: $msgs[]="您上傳的文件大于服務(wù)器限定值,上傳未成功"; break; case 3: $msgs[]="未能從本地完整上傳備份文件"; break; case 4: $msgs[]="從本地上傳備份文件失敗"; break; case 0: break; } if($msgs){show_msg($msgs);pageend();} $fname=date("Ymd",time())."_".sjs(5).".sql"; if (is_uploaded_file($_FILES['myfile']['tmp_name'])) { copy($_FILES['myfile']['tmp_name'], "./backup/".$fname);} if (file_exists("./backup/".$fname)) { $msgs[]="本地備份文件上傳成功"; if(import("./backup/".$fname)) {$msgs[]="本地備份文件成功導(dǎo)入數(shù)據(jù)庫"; unlink("./backup/".$fname);} else $msgs[]="本地備份文件導(dǎo)入數(shù)據(jù)庫失敗"; } else ($msgs[]="從本地上傳備份文件失敗"); show_msg($msgs); /****本地恢復(fù)結(jié)束*****/}/****************************************************/ /****************************主程序結(jié)束*/}/**********************************/ /*************************剩余分卷備份恢復(fù)**********************************/ if(!$_POST['act']&&$_SESSION['data_file']) { $filename="./backup/".$_SESSION['data_file']; if(import($filename)) $msgs[]="備份文件".$_SESSION['data_file']."成功導(dǎo)入數(shù)據(jù)庫"; else {$msgs[]="備份文件".$_SESSION['data_file']."導(dǎo)入失敗";show_msg($msgs);pageend();} $voltmp=explode("_v",$_SESSION['data_file']); $volname=$voltmp[0]; $volnum=explode(".sq",$voltmp[1]); $volnum=intval($volnum[0])+1; $tmpfile=$volname."_v".$volnum.".sql"; if(file_exists("./backup/".$tmpfile)) { $msgs[]="程序?qū)⒃?秒鐘后自動開始導(dǎo)入此分卷備份的下一部份:文件".$tmpfile.",請勿手動中止程序的運行,以免數(shù)據(jù)庫結(jié)構(gòu)受損"; $_SESSION['data_file']=$tmpfile; show_msg($msgs); sleep(3); echo ""; } else { $msgs[]="此分卷備份全部導(dǎo)入成功"; unset($_SESSION['data_file']); show_msg($msgs); } } /**********************剩余分卷備份恢復(fù)結(jié)束*******************************/ function import($fname) {global $d; $sqls=file($fname); foreach($sqls as $sql) { str_replace("\r","",$sql); str_replace("\n","",$sql); if(!$d->query(trim($sql))) return false; } return true; } function show_msg($msgs) { $title="提示:"; echo "
".$title." |
|
?

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

ThePhpfunctionSerialize () andunserialize () AreusedtoconvertcomplexdaTastructdestoresintostoraSandaBackagain.1.Serialize () c OnvertsdatalikecarraysorobjectsraystringcontainingTypeandstructureinformation.2.unserialize () Reconstruct theoriginalatataprom

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

The key to writing clean and easy-to-maintain PHP code lies in clear naming, following standards, reasonable structure, making good use of comments and testability. 1. Use clear variables, functions and class names, such as $userData and calculateTotalPrice(); 2. Follow the PSR-12 standard unified code style; 3. Split the code structure according to responsibilities, and organize it using MVC or Laravel-style catalogs; 4. Avoid noodles-style code and split the logic into small functions with a single responsibility; 5. Add comments at key points and write interface documents to clarify parameters, return values ??and exceptions; 6. Improve testability, adopt dependency injection, reduce global state and static methods. These practices improve code quality, collaboration efficiency and post-maintenance ease.

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas
