如何在更新数据库时刷新页面?

时间:2011-01-07 19:51:41

标签: php database refresh

如何检测对数据库所做的最新更新,并在发生更改时以静默方式刷新页面?

假设数据库访问如下:

$host = "localhost";
$username = "root";
$password = "root";
$db = mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db('ccr') or die(mysql_error());

任何想法和样品将不胜感激。谢谢。

6 个答案:

答案 0 :(得分:11)

这是我最近使用jQuery实现解决方案的方式。

每次发生重要更新时,PHP都会增加数据库中的字段。

<?php

//  Call this function when data changes
function update_clients()
{
    mysql_query( "UPDATE pageGen SET id = id + 1 LIMIT 1" );
}

//  Call this function to get the ID to pass to JavaScript
function get_update()
{
    $result = mysql_query( "SELECT id FROM pageGen LIMIT 1" );
    $update = mysql_result( $result, 0, 'id' );
    return $update;
}

?>

最初加载页面时,使用数据库中的数字填充JavaScript变量:

<script type="text/javascript">
var pageGenID = 25218603  //  generated by PHP
var processUpdate = function( response ) 
{
    if ( pageGenID < response ) 
    {
        replace_current_data_with_new_via_ajax();
        pageGenID = response;
    }
}
//  Compare our Page Generate ID against that of the server
var checkUpdates = function()
{
    serverPoll = setInterval( function()
    {
        $.get('script_to_return_latest_pageGenID.php', 
          { lastupdate: 1 }, 
          processUpdate, 'html');
    }, 10000 )
};

//  Check for updates every 10 seconds
$( document ).ready( checkUpdates );

</script>

答案 1 :(得分:2)

以下是我所做的并使用了您标记的答案,但我认为有一些事情需要改变。 在主页面中(如果数据库中发生了某些更改,则要刷新)。 我在我的数据库中创建了一个名为setting的表,在这个表中我创建了一个名为rowCounter的行。 当您检查的表中的行数已更改时,将更新rowCounter。 所以我的代码分成两个块。一个给我设置的数字&gt; rowCounter 第二个是给我表中当前数字的脚本。如果它们不相等,那么我刷新页面。

所以这是你需要的第一个文件
script_to_return_latest_pageGenID.php

// here you will make you connection query.

$result = mysql_query( "SELECT rowCounter FROM setting WHERE `id`='2'" );
$update = mysql_fetch_assoc($result);
        echo implode($update);

    $count=mysql_query("SELECT `id` FROM log_".$datestamp."")or die(mysql_error);
    $number =  mysql_num_rows($count);
//echo $number;

    $countFromSetting=mysql_query("SELECT `rowCounter` FROM setting  WHERE id='2'")or die(mysql_error);
    $numberFromSetting=implode(mysql_fetch_assoc($countFromSetting));

    if($number != $numberFromSetting)
        {
            mysql_query("UPDATE setting SET `rowCounter`='$number' WHERE `id`='2'")or die(mysql_error);

        }


在主页面中,您将编写我提供的两个代码块,您应该将它放在脚本之前。

$countFromSetting=mysql_query("SELECT `rowCounter` FROM setting  WHERE id='2'")or die(mysql_error);  
         $numberFromSetting=implode(mysql_fetch_assoc($countFromSetting));

在这段代码之后,你把每隔几秒钟查询php脚本的脚本,检查数字是否相等,它会刷新页面。

var pageGenID = "<?php echo $numberFromSetting; ?>"; 
var processUpdate = function( response ) {


var x=response;
//console.log(pageGenID); by removing the remarks you will see the compared numbers all the time.
//console.log(x);
if ( pageGenID != x ) 
{
    //replace_current_data_with_new_via_ajax();
    pageGenID = response;

    window.location.reload();       
   }
}

 var checkUpdates = function()
{
    serverPoll = setInterval( function()
  {

$.get('script_to_return_latest_pageGenID.php', 
      { lastupdate: 1 }, 
      processUpdate, 'html');
  }, 5000 ) };
    $( document ).ready( checkUpdates );

答案 2 :(得分:1)

我相信你在使用PHP时必须轮询数据库,PHP在长时间运行的进程中不存在,就像Java容器的情况一样,Java Web应用程序可以创建持久连接(理论上)你可能需要采用某种轮询机制,我的意思是在Javascript中设置一个计时器或者你的客户端代码定期发出请求,就提高效率而言如果需要你可以创建一个标志用户表并设置标志以指示自用户上次轮询以来数据库已失效,然后您的第一次检查将是自该用户上次更新以来是否发生了无效,而不是始终将所有内容发送给所有人。或者,我认为你需要放弃PHP来完成这项任务。

答案 3 :(得分:1)

IDEEA:

使用php渲染页面时,可以传递js变量,该变量保留了“当前数据库修订版本”(数字)。在页面上,你每隔30秒就进行一次ajax调用,一个女巫会回复一个新的当前数据库修订版。你测试那些2,如果你从ajax调用获得的那个更高,那么这意味着你需要重新加载页面。

在服务器端,您需要一个表来存储当前版本,每次从php进行查询时,您都会设置当前版本+1(在修订数据库表中)。当您从客户端收到ajax调用时,您从数据库中读取该号码并将其传递给cilent。您可以设置一个每天都会重置计数器的cronjob。

答案 4 :(得分:1)

严格来说,这是不可能的。如果更改了某些内容,MySQL不会将触发器重新启动回PHP。这是不可能的。此外,MySQL使用会话进行操作,因此即使它可以报告数据库中的更改,您也必须使用持久连接才能访问那种触发器。

现在,如果要执行select语句来检测数据库中的更改,那就不同了。根据您希望代码的“新”程度(取决于您希望它的兼容性),您可以使用以下任何一项来“轮询”PHP页面以检查更改:Comet,AJAX,a永远的框架,或HTML5的新WebSocket类。

虽然其他人说使用客户端通过存储在javascript中的数据库版本等变量来检测更改,但我建议您将该服务器保留为支持,因为知道如何注入javascript的人将能够更改该值,这可能会产生不希望的甚至是恶意的结果(甚至,取决于使用该值的方式,MySQL注入)。

答案 5 :(得分:1)

看起来你可以使用这样的东西,使用while循环。这不会 为大量用户工作(可能会崩溃PHP或SQL),并在某些时候 你必须重置脚本中的计数变量。:

 <?PHP
 //initialize the variables somewhere before any of the following. do not use these
 //variables for any other purpose other than the refresh routines:
 $a==0;
 $b==0;
 //First design code within a function to refresh the database a single iteration. 
 fuction refresh(){
     insert code here to output database;
      }

 //Now, call function refresh in a loop at the spot in the code 
 //where we want to refresh the database. where $b here is equal to 
 //the number of times you want the database to automatically refresh 
 //before having to reset the variables to 0. 

 $b==1 
 while ($b!==$a){
      refresh();
      $a==$a+1;
      } 

 ?>
相关问题