我是新手,请多关照:)
检查、检索和使用从 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/