PHP无限循环结果没有重复但更新

时间:2016-01-10 03:59:23

标签: php loops while-loop

我正在使用PHP创建后台进程,但我打算使用while(1)对其进行无限执行。结果显示但重复结果如下:

Result 1
text: Hi!
Result 2
text: Hello!
Result 1
text: Hi!
Result 2
text: Hello!

我不知道要做的是它不会重复,但会在某些内容发生变化时更新:

初次运行:

Result 1
text: Hi!
Result 2
text: Hello!

当有变化时:

Result 1
text: Hello!
Result 2
text: Hello!

上面的场景只是示例,但脚本已连接到数据库。

更新:我的代码。

<?php

    @ini_set("output_buffering", "Off");
    @ini_set('implicit_flush', 1);
    @ini_set('zlib.output_compression', 0);
    @ini_set('max_execution_time',1200);

    require("database.php");

    while(1){
        $sqlReports = "SELECT * FROM reports WHERE reports.process = 'F'";

        $resReports = odbc_exec($conn,$sqlReports);

        while($reports = odbc_fetch_array($resReports))
        {
            echo $reports['reportnum']. "</br>";

            $sqlStations = "SELECT * FROM stations";

            $resStations = odbc_exec($conn,$sqlStations);

            while($stations = odbc_fetch_array($resStations))
            {
                echo $stations['name']. "</br>";
            }

            sleep(1);
            flush();
            ob_flush();     
        }
    }

?>

代码结果:

1
station1
station2
station3
2
station1
station2
station3
3
station1
station2
station3
1
station1
station2
station3
2
station1
station2
station3
...

我想要的是它不会再显示显示的数字,但如果有如下更改,它会更新:

循环期间无更新:

1
station1
station2
station3
2
station1
station2
station3
3
station1
station2
station3

循环期间更新:

1
station1
station2
station5
2
station6
station2
station3
3
station7
station8
station9

1 个答案:

答案 0 :(得分:0)

如果我们查看您期望的输出,如果它与之前运行的脚本具有所有相同的站点,您似乎想跳过站点部分。

实现这样的想法

要么需要将站点的状态存储到临时存储中,下次再次运行脚本时,您可以查看临时存储并比较是否有更改,这样您就可以决定是包括站点部分。

您可以在站级别拥有一些标志,表示该站点条目对于特定报告是新的,如果您找到任何这样的新站点,您可以包括该站点,否则您可以跳过它。