及时从数据库中检索数据的时间间隔,并在数据到达时停止 [英] Timely interval to retrieve data from db, and stop when the data arrives

查看:97
本文介绍了及时从数据库中检索数据的时间间隔,并在数据到达时停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,请客气:)

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

What is the simplest solution for checking for, retrieving and using newly received data from a mysql database?

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

The database is being updated from an external api. Ideally I would get it as it arrives but using intervals could work too. On the first iteration and then once every 5 minutes if there is no new data meanwhile - do an action with the next entry of the array which might cause data to be updated within the next 5 min. But I want to make sure to stop everything if the new data has been received, and just perform some stuff in php.

最简单的解决方案是什么? php vs jquery/ajax吗?

what's the most simple solution? php vs jquery/ajax?

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

my proposed solution is in 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(在这种情况下,实际上不需要客户端).

My solution in the end is indeed in php and not ajax (there is really no need for client side in this situation).

这是轮廓:

    <?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
            }
        }
    ?>

这篇关于及时从数据库中检索数据的时间间隔,并在数据到达时停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆