Asked  7 Months ago    Answers:  5   Viewed   41 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.

 Answers

67

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>
Wednesday, March 31, 2021
 
WooDzu
answered 7 Months ago
96

I have found out the way to get around this problem and I can imagine it could be an issue for other VistaPanel/cPanel users of phpMyAdmin.

I had to log into the cPanel, change my password, logout, clear all my cookies and then log back in. When I logged back in and tried to connect to phpMyAdmin I could get in fine!

Wednesday, March 31, 2021
 
commonpike
answered 7 Months ago
51

Use location.reload():

$('#something').click(function() {
    location.reload();
});

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.

Tuesday, June 1, 2021
 
FWH
answered 5 Months ago
FWH
81

Had the same issue with PHPStorm on MAMP. Checked the port in localhost where MySQL was installed. It was 8889 and not the default 3306. Updating the port fixed the issue for me.

Tuesday, August 24, 2021
 
Sergey Snegirev
answered 2 Months ago
42

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.

Sunday, October 17, 2021
 
Zed
answered 1 Week ago
Zed
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :