dppb79372 2014-11-24 09:25
浏览 58
已采纳

在while循环中使用条件

I have this code

$dbh = new PDO('mysql:host=localhost;dbname=odesk', 'root', '123456');
$sth = $dbh->prepare("SELECT id,msisdn from new_r4 limit 1,10");
$sth2 = $dbh->prepare("SELECT status from flag where id = 1");
$sth->execute();
$sth2->execute();

while ($result = $sth->fetch(PDO::FETCH_ASSOC)) {
    $result2 = $sth2->fetch(PDO::FETCH_ASSOC);

    $flag = $result2['status'];
    $the_number = $result['msisdn'];
    $id = $result['id'];

    while ($flag == 0) {
        echo 'Waiting.......' . PHP_EOL;
        sleep(1);
    }
    //Part of the condition,just added
    while ($flag == 1) {
        echo $the_number . '   ' . $id .'  ' . PHP_EOL;
        sleep(1);
    }
}

which is a a cli script that displays some numbers from my address book if a certain condition is met.If a the flag is 0 then no number shall be displayed and when the flag is 1,then display the number.

The problem is,i can't find the right condition to use after

while ($flag == 0) {
    echo 'Waiting.......' . PHP_EOL;
    sleep(1);
}

The if , else and case do not wait up until the flag is 1.

What condition can i use to get the script to display the numbers when $flag == 1?.

  • 写回答

2条回答 默认 最新

  • dspv70887 2014-11-24 10:22
    关注

    You're using $result2 and $result1. Your $flag will always be the value id = 1 because the WHERE clause never changes - so either $flag will always be either 1 or another value (from your question, it's going to always be another value). Either change your query to join the table, or query whilst in the while loop.

    Assuming new_r4.id = flag.id

    SELECT r.`id`, r.`msisdn`, f.`status`
    FROM new_r4 r
    LEFT JOIN flag f
    ON r.id = f.id
    LIMIT 1, 10
    

    Change your code to become

    while ($result = $sth->fetch(PDO::FETCH_ASSOC)) {
    
        $flag = $result['status'];
        $the_number = $result['msisdn'];
        $id = $result['id'];
    

    Now all you need to do is check $flag is equal to 1, and you're golden.

    if($flag == 1) {
       echo $this_number . PHP_EOL;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么