php處理大量數(shù)據(jù),每處理一個數(shù)據(jù)返回客戶端顯示當(dāng)前狀態(tài)的方法。
類似于dedecms生成靜態(tài)頁
想法:
1.客戶端發(fā)送請求
2.服務(wù)器端接受請求,開始統(tǒng)計(jì)所需處理的數(shù)據(jù)量
3.將所需處理數(shù)據(jù)按一定規(guī)則排列,發(fā)送到服務(wù)器處理端
4.服務(wù)器處理端處理了第一個數(shù)據(jù),將處理結(jié)果經(jīng)過一定處理后發(fā)送給客戶端
5.客戶端接收到結(jié)果,自動將處理結(jié)果顯示并發(fā)送到服務(wù)器
6.服務(wù)器接收到處理結(jié)果 將它轉(zhuǎn)發(fā)到服務(wù)器處理端
7.處理端繼續(xù)處理結(jié)果...
8.循環(huán)4-7步驟,直到處理完畢
實(shí)驗(yàn)過程:
1.創(chuàng)建數(shù)據(jù)庫和表
create databases handle;create table user(id int unsigned not null auto_increment primary key,name varchar(8),sex tinyint(1) default '1',score int not null,state tinyint(1));
2.向表中添加數(shù)據(jù)(不示例)
3.創(chuàng)建index.html客戶端,a.php服務(wù)端1,b.php服務(wù)端2
Index.html:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>客戶端</title></head><body><button onclick="send('a.php?state=0')">開始請求</button><div style="position: fixed;width: 500px;height: 300px;top: 100px;background: gray"><span style="color: white;font-size: 20px;"></span></div><script type="text/javascript" src="./jquery-1.10.2.min.js"></script><script type="text/javascript">//創(chuàng)建一個模態(tài)框function display(value){$('span').html(value);}//ajaxfunction send(dizhi){$.ajax({type: "get",url: dizhi,success: function(msg){var arr=JSON.parse(msg);console.log(arr);//alert(arr.value);var tishi="已經(jīng)處理 "+arr.now +"個,共"+arr.all+"個";display(tishi);if(arr.now!=arr.all){send("a.php?now="+arr.now+"&all="+arr.all);}else{alert("完成!");}}});}</script></body></html>
a.php:
<?phprequire('./dbconfig.php');$link=mysql_connect(HOST,USER,PASS) or die('數(shù)據(jù)庫鏈接失敗');mysql_select_db(DBNAME);/*查詢數(shù)據(jù)$sql="select * from user";$result=mysql_query($sql);$row=mysql_fetch_assoc($result);var_dump($row);*//*循環(huán)插入for($i=3;$i<=100;$i++){$sql= "insert into user(name,score,state) values('z".$i."',".$i.",1)";mysql_query($sql);}*//*查詢需要處理的數(shù)據(jù)總數(shù)*///isset($_GET['state'])?$_GET['state']:0;if(isset($_GET['state'])){$sql="select count(*) from user";$result=mysql_query($sql);$all=mysql_result($result,0);$now=0;header("Location: b.php?all={$all}&now=0");}else{header("Location: b.php?all={$_GET['all']}&now={$_GET['now']}");}/*返回當(dāng)前處理的數(shù)據(jù)*/
b.php:
<?phprequire('./dbconfig.php');$link=mysql_connect(HOST,USER,PASS) or die('數(shù)據(jù)庫鏈接失敗');mysql_select_db(DBNAME);/*返回當(dāng)前處理的數(shù)據(jù)*///$id=$_GET['id'];//獲取將要處理的id$now=$_GET['now'];//已經(jīng)處理的個數(shù)$all=$_GET['all'];//總共要處理的個數(shù)$sql="select score from user limit {$now},1";$result=mysql_query($sql);$value=mysql_result($result, 0);$now++;$arr=array('now'=>$now,'all'=>$all,'value'=>$value);//print_r($arr);echo json_encode($arr);
dbconfig.php:
<?phpdefine('HOST','127.0.0.1');define('USER', 'root');define('PASS','root');define('DBNAME','handle');
以上所述是小編給大家分享的使用PHP處理數(shù)據(jù)庫數(shù)據(jù)如何將數(shù)據(jù)返回客戶端并顯示當(dāng)前狀態(tài),希望對大家有所幫助!