Maison > Article > développement back-end > php处理数据库数据,每处理一个数据返回客户端显示当前状态的方法。,数据库当前状态_PHP教程
php处理大量数据,每处理一个数据返回客户端显示当前状态的方法。
类似于dedecms生成静态页
想法:
实验过程:
1.创建数据库和表
<span>create</span><span> databases handle; </span><span>create</span> <span>table</span> <span>user</span><span>( id </span><span>int</span> unsigned <span>not</span> <span>null</span> auto_increment <span>primary</span> <span>key</span><span>, name </span><span>varchar</span>(<span>8</span><span>), sex </span><span>tinyint</span>(<span>1</span>) <span>default</span> <span>'</span><span>1</span><span>'</span><span>, score </span><span>int</span> <span>not</span> <span>null</span><span>, state </span><span>tinyint</span>(<span>1</span><span>) );</span>
2.向表中添加数据(不示例)
3.创建index.html客户端,a.php服务端1,b.php服务端2
Index.html:
<span><!</span><span>DOCTYPE html</span><span>></span> <span><</span><span>html </span><span>lang</span><span>="en"</span><span>></span> <span><</span><span>head</span><span>></span> <span><</span><span>meta </span><span>charset</span><span>="UTF-8"</span><span>></span> <span><</span><span>title</span><span>></span>客户端<span></</span><span>title</span><span>></span> <span></</span><span>head</span><span>></span> <span><</span><span>body</span><span>></span> <span><</span><span>button </span><span>onclick</span><span>="send('a.php?state=0')"</span><span>></span>开始请求<span></</span><span>button</span><span>></span> <span><</span><span>div </span><span>style</span><span>="position: fixed;width: 500px;height: 300px;top: 100px;background: gray"</span><span>></span> <span><</span><span>span </span><span>style</span><span>="color: white;font-size: 20px;"</span><span>></</span><span>span</span><span>></span> <span></</span><span>div</span><span>></span> <span><</span><span>script </span><span>type</span><span>="text/javascript"</span><span> src</span><span>="./jquery-1.10.2.min.js"</span><span>></</span><span>script</span><span>></span> <span><</span><span>script </span><span>type</span><span>="text/javascript"</span><span>></span> <span>//</span><span>创建一个模态框</span> <span>function</span><span> display(value){ $(</span><span>'</span><span>span</span><span>'</span><span>).html(value); } </span><span>//</span><span>ajax</span> <span>function</span><span> send(dizhi){ $.ajax({ type: </span><span>"</span><span>get</span><span>"</span><span>, url: dizhi, success: </span><span>function</span><span>(msg){ </span><span>var</span><span> arr</span><span>=</span><span>JSON.parse(msg); console.log(arr); </span><span>//</span><span>alert(arr.value);</span> <span>var</span><span> tishi</span><span>=</span><span>"</span><span>已经处理 </span><span>"</span><span>+</span><span>arr.now </span><span>+</span><span>"</span><span>个,共</span><span>"</span><span>+</span><span>arr.all</span><span>+</span><span>"</span><span>个</span><span>"</span><span>; display(tishi); </span><span>if</span><span>(arr.now</span><span>!=</span><span>arr.all){ send(</span><span>"</span><span>a.php?now=</span><span>"</span><span>+</span><span>arr.now</span><span>+</span><span>"</span><span>&all=</span><span>"</span><span>+</span><span>arr.all); }</span><span>else</span><span>{ alert(</span><span>"</span><span>完成!</span><span>"</span><span>); } } }); } </span><span></</span><span>script</span><span>></span> <span></</span><span>body</span><span>></span> <span></</span><span>html</span><span>></span>
a.php:
<?<span>php </span><span>require</span>('./dbconfig.php'<span>); </span><span>$link</span>=<span>mysql_connect</span>(HOST,USER,PASS) or <span>die</span>('数据库链接失败'<span>); </span><span>mysql_select_db</span><span>(DBNAME); </span><span>/*</span><span> 查询数据 $sql="select * from user"; $result=mysql_query($sql); $row=mysql_fetch_assoc($result); var_dump($row); </span><span>*/</span> <span>/*</span><span> 循环插入 for($i=3;$i<=100;$i++){ $sql= "insert into user(name,score,state) values('z".$i."',".$i.",1)"; mysql_query($sql); } </span><span>*/</span> <span>/*</span><span>查询需要处理的数据总数</span><span>*/</span> <span>//</span><span>isset($_GET['state'])?$_GET['state']:0;</span> <span>if</span>(<span>isset</span>(<span>$_GET</span>['state'<span>])){ </span><span>$sql</span>="select count(*) from user"<span>; </span><span>$result</span>=<span>mysql_query</span>(<span>$sql</span><span>); </span><span>$all</span>=<span>mysql_result</span>(<span>$result</span>,0<span>); </span><span>$now</span>=0<span>; </span><span>header</span>("Location: b.php?all={<span>$all</span>}&now=0"<span>); }</span><span>else</span><span>{ </span><span>header</span>("Location: b.php?all={<span>$_GET</span>['all']}&now={<span>$_GET</span>['now']}"<span>); } </span><span>/*</span><span>返回当前处理的数据</span><span>*/</span>
b.php:
<?<span>php </span><span>require</span>('./dbconfig.php'<span>); </span><span>$link</span>=<span>mysql_connect</span>(HOST,USER,PASS) or <span>die</span>('数据库链接失败'<span>); </span><span>mysql_select_db</span><span>(DBNAME); </span><span>/*</span><span>返回当前处理的数据</span><span>*/</span> <span>//</span><span>$id=$_GET['id'];//获取将要处理的id</span> <span>$now</span>=<span>$_GET</span>['now'];<span>//</span><span>已经处理的个数</span> <span>$all</span>=<span>$_GET</span>['all'];<span>//</span><span>总共要处理的个数</span> <span>$sql</span>="select score from user limit {<span>$now</span>},1"<span>; </span><span>$result</span>=<span>mysql_query</span>(<span>$sql</span><span>); </span><span>$value</span>=<span>mysql_result</span>(<span>$result</span>, 0<span>); </span><span>$now</span>++<span>; </span><span>$arr</span>=<span>array</span><span>( </span>'now'=><span>$now</span>, 'all'=><span>$all</span>, 'value'=><span>$value</span><span> ); </span><span>//</span><span>print_r($arr);</span> <span>echo</span> json_encode(<span>$arr</span>);
dbconfig.php:
<?php define('HOST','127.0.0.1'); define('USER', 'root'); define('PASS','root'); define('DBNAME','handle');
以上是全部~