Viewed   75 times

How can I detect the latest updates made to a database and silently refresh a page when a change occurs?

Let's say the database access looks like:

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

Any ideas and samples would be appreciated. Thank you.



This is how I recently implemented a solution using jQuery.

PHP increments a field in the database every time a significant update occurs.


//  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 ) 
        pageGenID = response;
//  Compare our Page Generate ID against that of the server
var checkUpdates = function()
    serverPoll = setInterval( function()
          { lastupdate: 1 }, 
          processUpdate, 'html');
    }, 10000 )

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

Friday, December 23, 2022

If you make your db connection a Singleton class and you have an __autoload() function, then you can just have a dbconnection property in each of your classes which is set during construction.


class DBConnection{

  private static $instance;

  private function __construct(){
    //make your db connnection

  public static function get_connection(){
    if(empty self::$instance) self::$instance = new DBConnection;
    return self::$instance;

class Foo {

  private $db;

  function __construct(){
Monday, October 17, 2022

Use location.reload():

$('#something').click(function() {

The reload() function takes an optional parameter that can be set to true to force a reload from the server rather than the cache. The parameter defaults to false, so by default the page may reload from the browser's cache.

Friday, October 7, 2022

It turns out that, because the URL we're using sets a cookie, and Hootsuite creates a frame, that IE won't trust the third party cookie (our site). So I've added some framebuster code to the page, and triggered it to happen "immediately" if the browser is IE. Code is below:

<meta http-equiv="refresh" content="15;url=<?php echo $url ?>" />
<script type="text/javascript">
var timeout = 1;
if (navigator.userAgent.match(/MSIE/)) { timeout = 1; } else { timeout = 14500; }
setTimeout('if (top != self) top.location.replace(self.location.href)', timeout);

Maybe this will help some other random user out there some day.

Sunday, September 25, 2022

You should probably override your onResume() to check if the list of alarms have been refreshed so that whenever you come to/come back to your ListActivity, it'll get updated.

Saturday, November 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :