Home > Backend Development > PHP Tutorial > PHP processes database data, and each time a piece of data is processed, it returns a method to the client to display the current status. , the current status of the database_PHP tutorial

PHP processes database data, and each time a piece of data is processed, it returns a method to the client to display the current status. , the current status of the database_PHP tutorial

WBOY
Release: 2016-07-12 08:59:11
Original
846 people have browsed it

php processes database data and returns a method to the client to display the current status for each piece of data processed. , the current status of the database

php processes a large amount of data, and each time a piece of data is processed, it returns a method to the client to display the current status.

Similar to dedecms to generate static pages

Ideas:

Experimental process:

1. Create database and tables

<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>
Copy after login

2. Add data to the table (not examples)

3. Create index.html client, a.php server 1, b.php server 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>
Copy after login

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>
Copy after login

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>);
Copy after login

dbconfig.php:

<?php
	define('HOST','127.0.0.1');
	define('USER', 'root');
	define('PASS','root');
	define('DBNAME','handle');
Copy after login

That’s all~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1099422.htmlTechArticlephp processes database data, and each time a piece of data is processed, it returns a method to the client to display the current status. , the current status of the database. PHP processes a large amount of data, and each data processed is returned to the client for display...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template