为了账号安全,请及时绑定邮箱和手机立即绑定

MySQL PHP 如何分批量的从数据库循环读取数据?

MySQL PHP 如何分批量的从数据库循环读取数据?

PHP
天涯尽头无女友 2019-03-06 09:09:11
假如每次从数据库读取五条,该怎么一直让程序循环下去,直到全都循环完成。加个 $a = true; //然后用while循环 //在数据库里设计个字段,为判断有没有取出来过这条数据的字段,默认为没取出来过 //然后用foreach把每次取出来的每条数据都标记为已取出过,直到所有数据都被标记为已取出过来,循环停止。 这样的逻辑对吗?
查看完整描述

11 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

<?php

$read  =  ture;
$limit =  0;
while($read)
{    

    $sql = "select id,name from table limit {$limit},5";
    $data = $db->select($sql); 
    //todo 你的业务逻辑
    
    if(!data)
    {    
        $read = false;
        break;数据为空,直接停止了循环
    }
   
    $limit += 5;每次获取后面5条,直至数据查询为空.

}

查看完整回答
反对 回复 2019-03-18
?
慕侠2389804

TA贡献1719条经验 获得超6个赞

你用limit呀。然后记录id,直到id大于maxid不就可以了么???

查看完整回答
反对 回复 2019-03-18
?
慕工程0101907

TA贡献1887条经验 获得超5个赞

可以用分页查询的原理啊。。

$count = '100';//总条数
$strip = 0;
$num = $count/5;//循环多少次
for($i=1;$i<=$num;$i++)
{
 $sql = "select * from `table` order by id limit ".$strip.",5";
 $strip += 5;
}
查看完整回答
反对 回复 2019-03-18
?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

$id = 0;
while(true){
   $arr = $db->query('select id,name from users where id>'.$id.' limit 5');
   if(count($arr) == 0) {
     break;
   }
   // 处理逻辑 
   
  if(count($arr) < 5) {
     break;
   }
   $id = $arr[4]['id']; // 取最后一个id
}
查看完整回答
反对 回复 2019-03-18
?
子衿沉夜

TA贡献1828条经验 获得超3个赞

最好for循环里面sleep下,让cpu休息一下

查看完整回答
反对 回复 2019-03-18
?
心有法竹

TA贡献1866条经验 获得超5个赞

I will suggest you using a Loop when enters the name. Make sure to use ForEach Loop. Moreover let me tell you that you can Check Chase Verify.

查看完整回答
反对 回复 2019-03-18
?
阿晨1998

TA贡献2037条经验 获得超6个赞

I have read your article and found some information that will help me to increase my knowledge. however thanks for sharing your idea with us. You can get more info here Super Bowl Live Stream

查看完整回答
反对 回复 2019-03-18
?
HUH函数

TA贡献1836条经验 获得超4个赞

WordPress uses PHP, with SQL queries in PHP markup to store and export data from MySQL databases. For example, if you are a member of a site that uses WordPress's management system, SQL is used to log in, obtain a membership ID, verify that you have an active membership, and ensure that the front-end is displaying. Correct profile data.
PHP and SQL work in parallel in WordPress, helping you create dynamic content based on a variety of factors, such as your IDs and user roles. This allows you to do many things like hiding or show content to specific users, like admins, editors or subscribers. These tasks can not be completed if SQL and MySQL.
192.168.1.1

查看完整回答
反对 回复 2019-03-18
?
慕沐林林

TA贡献2016条经验 获得超9个赞

thank you you can visit my site for more information
链接描述

查看完整回答
反对 回复 2019-03-18
?
智慧大石

TA贡献1946条经验 获得超3个赞

Thanks for the comments. It's hard to work on PHP. I started my Oscar portal on Wordpress and no its getting difficult to carry on.

查看完整回答
反对 回复 2019-03-18
  • 11 回答
  • 0 关注
  • 654 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信