How to do a real-time database poll in MySQL / PHP?

I have a Ruby script that constantly updates the MySQL database. I want to show " mysql_num_rows()" in real time. Since the entry is entered into the Ruby script database, I want the PHP script to update the mysql_num_row () counter in real time.

I tried to use <meta http-equiv="refresh" content="5">, but I do not think this is the best solution.

Does anyone have a better solution?

+3
source share
5 answers

Use JavaScript on the page to periodically make calls to the server and receive some data. Using the jQuery library for cross-browser AJAX support, you simply do the following:

jQuery(function($){
  setInterval(function(){
    $.get( '/getrows.php', function(newRowCount){
      $('#rowcounter').html( newRowCount );
    });
  },5000); // 5000ms == 5 seconds
});

This will make a request to your server every 5 seconds and attach the result to what you send back to the element with the id rowcounter, for example.

<p>There are <span id='rowcounter'>xx</span> rows in the DB.</p>
+3
source

I would use the ajax updater to continue polling the page that your mysql_num_row () prints. A prototype would be a good solution: http://www.prototypejs.org/api/ajax/updater

+1
source

PHP javascript - , wajiw , ajax /, .

( ) /.

0

, script cron ( script ). JS, . mysql.

0

Source: https://habr.com/ru/post/1775327/


All Articles