刷新页面,直到新数据添加到数据库中


refreshing page until new data added to database

我想用SetInterval()每隔几秒刷新一个PHP页面;如果有数据添加到数据库中,它会停止刷新页面。因此,当它刷新我的PHP页面时,它总是检查是否有新数据添加到数据库中。

我正在尝试使用这种逻辑,但它不起作用…

$query = mysql_num_rows(SELECT id_example FROM table_example);
$a = count($query);
$b = $a+1;
IF($a==$b)
{
Stop_refreshing_the_page;
}
else
{
Refresh_with_setInterval();
}

有没有人可以建议我更好的逻辑/算法/代码示例??

哪个部分不工作?

此外,如果每次刷新整个页面,setInterval可能不是您想要的—似乎简单的setTimeout和重新加载就可以做到这一点,然后在您有db结果时简单地不打印它。

编辑OP

我认为这不是有效的PHP,但你应该明白。

$previous_count = $_GET['previous_count'];
$query = mysql_num_rows(SELECT id_example FROM table_example);
$result_count = count($query);
if ($previous_count == $result_count)
{
    // this should render some javascript on the page including $result_count
}

生成的javascript应该是这样的:

<script type='text/javascript'>
    window.setTimeout( function(){
        window.location = window.location.pathname + "?previous_count=<?php echo $previous_count; ?>";
    }, 2000);
</script>