您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)使用PHP處理數(shù)據(jù)庫數(shù)據(jù)怎么將數(shù)據(jù)返回客戶端并顯示當(dāng)前狀態(tài)的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
php處理大量數(shù)據(jù),每處理一個數(shù)據(jù)返回客戶端顯示當(dāng)前狀態(tài)的方法。
類似于dedecms生成靜態(tài)頁
想法:
1.客戶端發(fā)送請求
2.服務(wù)器端接受請求,開始統(tǒng)計所需處理的數(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 > <span ></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); } //ajax function 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:
<?php require('./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:
<?php require('./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:
<?php define('HOST','127.0.0.1'); define('USER', 'root'); define('PASS','root'); define('DBNAME','handle');
感謝各位的閱讀!關(guān)于“使用PHP處理數(shù)據(jù)庫數(shù)據(jù)怎么將數(shù)據(jù)返回客戶端并顯示當(dāng)前狀態(tài)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。