> 백엔드 개발 > PHP 튜토리얼 > php处理数据库数据,每处理一个数据返回客户端显示当前状态的方法。,数据库当前状态_PHP教程

php处理数据库数据,每处理一个数据返回客户端显示当前状态的方法。,数据库当前状态_PHP教程

WBOY
풀어 주다: 2016-07-12 08:59:11
원래의
841명이 탐색했습니다.

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');
로그인 후 복사

 

以上是全部~

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1099422.htmlTechArticlephp处理数据库数据,每处理一个数据返回客户端显示当前状态的方法。,数据库当前状态 php处理大量数据,每处理一个数据返回客户端显示...
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿