【问题标题】:How to make infinite "while" loop in php如何在php中制作无限的“while”循环
【发布时间】:2018-11-24 12:41:41
【问题描述】:

我想创建每秒检查数据库中传入订单并做一些事情的服务。现在我正在使用 php5.6 和 mysql,但我不确定这是否有好处。 对不起我的代码,但现在我正在这样做:

 for ($x=0; $x < 999999999999; $x++)
 {



$query1 = mysql_query("SELECT * FROM `orders`");
while ($row = mysql_fetch_assoc($query1)) {
//doing some stuff here
}
sleep(1);
}

它可以工作大约 12 小时,并且在出现错误时崩溃。 有没有更好的解决方案,或者选择不同的语言更好?

【问题讨论】:

标签: php mysql service while-loop infinite-loop


【解决方案1】:

您的 999999999999 将被接通。

试试这样的:

<?php

    $link = mysqli_connect("localhost", "username", "password", "database");
    if(!$link) {
        die("Could not connect to MySQL database.");
    }
    while(1) {
        $query = "SELECT * FROM YOUR_TABLE";
        $res = mysqli_query($link, $query);
        if(!$res) {
            die("Could not execute query: " . mysqli_error($link));
        }
        while($row = mysqli_fetch_assoc($res)) {
            // Do stuff here
        }
        sleep(1);
    }

【讨论】:

    猜你喜欢
    • 2011-11-19
    • 2014-05-30
    • 1970-01-01
    • 2011-10-20
    • 2014-03-29
    • 1970-01-01
    • 2016-08-28
    • 2012-10-29
    • 2012-12-24
    相关资源
    最近更新 更多