2011-01-07 61 views
10

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

比方说,对数据库的访问是这样的:

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

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

回答

9

这就是我最近如何使用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

首先,JQuery的补充方式对于这样一个简单的任务来说,开销太多了。如果你要使用AJAX路线,只需使用一个XMLHttpRequest对象。另外,就像我在我的文章中指出的那样,由于JavaScript或MySQL注入的风险增加,请保留诸如`pageGenID` *服务器端*之类的东西。 – Qix 2011-01-07 20:26:23

0

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

0

IDEEA:

当您呈现PHP页面中,你可以通过一个js变量女巫保持一个“当前的数据库版本”(数字)。在这个页面上,你每30秒就会发出一次ajax调用,一个女巫可以接受一个新的当前数据库版本。你测试这两个,如果你从ajax调用得到的那个更高,那么这意味着你需要重新加载页面。

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

1

严格地说,这是不可能的。如果事情发生变化,MySQL不会触发PHP返回PHP。这是不可能的。此外,MySQL使用会话进行操作,因此即使它可以报告数据库中的更改,也必须使用持久连接才能访问该类型的触发器。

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

虽然其他人说使用客户端通过像JavaScript中存储的数据库版本这样的变量来检测更改,但我建议您仅仅因为知道如何注入javascript的人可以更改该值,这可能会产生不希望的甚至恶意的结果(甚至取决于如何使用该值,MySQL注入)。

2

这是我做了什么,我用你标记的答案,但我认为有几件事要改变。主页中的 (如果数据库中的某些内容发生更改,则要刷新该页)。 我在我的数据库中创建了一个名为setting的表,在这个表中我创建了一个名为rowCounter的行。 当您检查的表中的行数已更改时,rowCounter正在更新。 所以我的代码是在两个块。一个给我从设置数字> 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); 
0

这似乎就像你可以使用这样的事情,用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; 
     } 

?> 
相关问题