English 中文(简体)
在更新数据库时,我怎么能够更新一页?
原标题:How can I refresh a page when a database is updated?

我怎么能发现对数据库的最新更新,在变化发生时静默地更新一页?

让我说,数据库的存取情况如下:

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

任何想法和样本都会受到赞赏。 谢谢。

最佳回答

这就是我最近如何利用“贱民”实施解决办法。

每次更新 重大时,都向数据库输入一个领域。

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

?>

When the page is initially loaded, populate a JavaScript variable with a number from the database:

<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>
问题回答

Here is what I did and used the answer you marked but I think there is a few things to change there. In the main page ( that you want to refresh if something changed in the database). I created a table in my database called setting, in this table i created a row that called rowCounter. the rowCounter is being updated when the numbers of rows in the table you check has been changed. so my code split in two blocks. one that giving me the number from the setting > rowCounter and the second is the script that give me the current number in the table. if they are not equal then I refresh the page.

so this is the first file you need
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);

        }


In the main page you will write the two blocks of code I provided and you should put it before the script.

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

After this code you put the script that will query every few seconds the php script, check if the numbers are not equal it will refresh the page.

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 );

我认为,当你使用PHP时,PHP在很长的运行过程中就一定会污染数据库,如在贾瓦集装箱中, Java网应用会形成一种持续的联系(理论上),你很可能需要使用某种投票机制,据此,我在 Java文中设置一个时记,或者不管你的客户代码是定期提出这种要求,因为如果需要,你可以在用户桌上打上国旗,并挂着国旗显示数据库已经失效,因为用户最终被污染,那么,那么,首先检查一下,如果该用户是最后一次使用,而不是向每个人发送所有时间,那么就会发生故障。 或者,我认为你需要放弃执行这项任务的购买力平价。

Ideea:

当你与php一起翻开一页时,你可以通过一个 j变式的巫术,保持“当前数据库修订”(编号)。 在网页上,每30秒钟就发出一次大礼,对当前数据库进行新的修订,以便发言。 如果你对这2人进行测试,如果你从阿加克斯呼吁中听到的话,那就意味着你需要重新上下页。

在服务器方面,你需要一张表格储存目前的修订稿,每当你从实验室提出质询时,你就会确定目前的修订+1(修订数据库表)。 当收到客户的传闻时,你从数据库中读到该号码,然后把该号码转至借方。 你们可以 set起 cr,每天重新打起反面。

严格地说,这是不可能的。 如果发生某种变化,我的SQL不会将火力推回购买力平价。 这是不可能的。 而且,我的SQL利用会议开展工作,即使能够报告数据库的变更,你也不得不使用一种持续的联系,以便你能够进入这种触发点。

现在,如果你想执行某些说明,以发现数据库的变化,情况就有所不同。 视“新”如何要求您的代码(取决于您的相容性)而定,您可使用以下任何一页“政策”来核对变化: Comet, AJAX, a forever framework, or hexachloro5 s new WebSocket/code>。

其他人则利用客户通过储存在javascript中的数据库版本等变数来发现变化,但我建议,你离开该服务器,仅仅因为知道如何注射ava印的人能够改变这种价值,从而可能产生不理想或甚至恶意的结果(即使取决于这一价值是如何使用的,MySQL注射)。

It would seem like you could use something like this, using a while loop. This won t work for massive amounts of users (will probably crash PHP or SQL), and at some point you will have to reset the count variables in your script.:

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

 ?>




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...