Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I refresh a page when a database is updated?

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.

like image 966
omc11 Avatar asked Jan 07 '11 19:01

omc11


People also ask

How do I trigger a refresh page?

You can use the location. reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button. The reload() method is the main method responsible for page reloading.

How do you refresh a page in SQL?

With your focus in the Query window, select Edit --> IntelliSense --> Refresh Local Cache.

How do I refresh the current page in Wordpress?

To force a refresh, just navigate to “Tools”, click on “Force Refresh” and click the button that says, “Refresh Site.”

How do you refresh a page in HTML?

Window location.reload() The reload() method reloads the current document. The reload() method does the same as the reload button in your browser.


1 Answers

This is how I recently implemented a solution using jQuery.

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

<?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>
like image 164
Kalessin Avatar answered Sep 17 '22 17:09

Kalessin