php - 定时从db中获取数据,数据到达时停止

标签 php javascript jquery mysql logic

我是新手,请多关照:)

检查、检索和使用从 mysql 数据库接收到的新数据的最简单解决方案是什么?

数据库正在从外部 api 更新。理想情况下,我会在它到达时得到它,但使用间隔也可以。 在第一次迭代中,如果同时没有新数据,则每 5 分钟一次 - 对数组的下一个条目执行操作,这可能会导致数据在接下来的 5 分钟内更新。但我想确保在收到新数据时停止所有操作,并只在 php 中执行一些操作。

什么是最简单的解决方案? php 与 jquery/ajax 的对比?

我提出的解决方案是在 jquery 中:

        <script>
            var newdata = false;
            if(newdata === false){
                setTimeout (function(){
                    $.each(array, function(){
                            $.post('checkdb.php',data,function(resp){
                                          if(resp){
                                          newdata=resp;
                                          return newdata;                                                  
                                          }
                                          else{
                                          $.post('doaction.php',data);
                                          // cause a potential update within the next 5 min
                                          }
                            });
                    });
                }, 300000); 
            }
            else{
                // move newdata back to php and (then) do something with response
            }
        </script>

谢谢! :)

最佳答案

我最终的解决方案确实是在 php 而不是 ajax(在这种情况下真的不需要客户端)。

大纲如下:

    <?php
        while (something is true){
            //do stuff

            flush(); // execute the stuff you did until now
            sleep(300); // wait 5 min
            // now check database and retrieve new data, if any, and insert into $result 

            if (isset($result)){
                //do stuff with the $result
                break; // get out of the loop
            }
        }
    ?>

关于php - 定时从db中获取数据,数据到达时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8621339/

相关文章:

javascript - jQuery ui 可排序 : items option hash selection not being selective after changes in DOM element

javascript - 查看页面时更改 CSS 代码

javascript - onMouseOut 事件在我的 HTML 中不起作用

javascript - 检查数据库中是否存在电子邮件文本框值

php - 如何将诸如 – 之类的 HTML 实体转换为其等效字符?

php - 加载 Symfony 2 应用程序时故障排除 "failed to load external entity"

javascript - 蒙戈聚合

javascript - 需要在ajax上传递两个元素值

javascript - ng-click 不使用 Controller-As 语法触发

php - OAuth 2 适用于我拥有的移动客户端