0

I want my client side web to auto-refresh when data in my Database updated, when adding or deleting data I have succeeded however when the data changed, it still fails.

This is my code for checking data from database:

<script> 
var row1 = "<?php echo $variable; ?>";

var processUpdate = function( response ) {
    var x = response;
    //console.log(x);
    if (row1 != x) {
        window.location.reload();
    }
}

var checkUpdates = function() {
    serverPoll = setInterval(function() {
        $.get('check.php', { lastupdate: 1 }, processUpdate, 'html');
    }, 1000)
};

$(document).ready(checkUpdates);

</script>

check.php:

$query = mysqli_query($koneksi, "SELECT * FROM table");
$number = mysqli_num_rows($query);
echo $number;

What should I change to be automatically refreshed if every data in the table is changed?

3
  • Too broad. But there's a db out there that'd help you with that task: rethinkdb.com . With mysql you could keep a counter somewhere, and increment it when a change is made. When the last value of the counter that you get early diverge from the actual counter value, you know that something changed. You can update then. Commented Sep 7, 2017 at 8:52
  • thank you i will try according to your suggestion Commented Sep 7, 2017 at 8:57
  • You can also use @Federkun 's approach in combination with REDIS and use it as cache for the counter. Commented Sep 7, 2017 at 9:22

1 Answer 1

1

You can use trigger that will insert some info about each table update in another table, and then just query the num rows on 'changes' table in a similar way you check for new ones here:

DELIMITER //
CREATE TRIGGER table_update_trigger AFTER UPDATE ON table
  FOR EACH ROW BEGIN
    INSERT INTO table_history
    (
      change
    )

    (
      NEW.some_value,

    );
  END
//

The advantage of this solution is you don't need to introduce/rely on/maintain any other db system like Redis and the checking code is not responsible for keeping and updating any counters and queries for updates, inserts and deletes in a similar fashion. Also you might extend the table_history table to log all the fields you are interested in in terms of tracking changes and end up having useful changelog for the purpose of the application.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.