《PHP學(xué)習(xí):使用PHP處理數(shù)據(jù)庫數(shù)據(jù)如何將數(shù)據(jù)返回客戶端并顯示當(dāng)前狀態(tài)》要點(diǎn):
本文介紹了PHP學(xué)習(xí):使用PHP處理數(shù)據(jù)庫數(shù)據(jù)如何將數(shù)據(jù)返回客戶端并顯示當(dāng)前狀態(tài),希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP實(shí)戰(zhàn)php處理大量數(shù)據(jù),每處理一個數(shù)據(jù)返回客戶端顯示當(dāng)前狀態(tài)的辦法.
PHP實(shí)戰(zhàn)類似于dedecms生成靜態(tài)頁
PHP實(shí)戰(zhàn)想法:
PHP實(shí)戰(zhàn)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步驟,直到處理完畢
PHP實(shí)戰(zhàn)實(shí)驗(yàn)過程:
PHP實(shí)戰(zhàn)1.創(chuàng)建數(shù)據(jù)庫和表
PHP實(shí)戰(zhàn)
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)
);
PHP實(shí)戰(zhàn)2.向表中添加數(shù)據(jù)(不示例)
PHP實(shí)戰(zhàn)3.創(chuàng)建index.html客戶端,a.php服務(wù)端1,b.php服務(wù)端2
PHP實(shí)戰(zhàn)Index.html:
PHP實(shí)戰(zhàn)
<!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);
}
//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>
PHP實(shí)戰(zhàn)a.php:
PHP實(shí)戰(zhàn)
<?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ù)*/
PHP實(shí)戰(zhàn)b.php:
PHP實(shí)戰(zhàn)
<?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);
PHP實(shí)戰(zhàn)dbconfig.php:
PHP實(shí)戰(zhàn)
<?php
define('HOST','127.0.0.1');
define('USER', 'root');
define('PASS','root');
define('DBNAME','handle');
PHP實(shí)戰(zhàn)以上所述是小編給大家分享的使用PHP處理數(shù)據(jù)庫數(shù)據(jù)如何將數(shù)據(jù)返回客戶端并顯示當(dāng)前狀態(tài),希望對大家有所贊助!
歡迎參與《PHP學(xué)習(xí):使用PHP處理數(shù)據(jù)庫數(shù)據(jù)如何將數(shù)據(jù)返回客戶端并顯示當(dāng)前狀態(tài)》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/7541.html