3

I have a Ruby script which is constantly updating a MySQL database. I want to show the "mysql_num_rows()" in realtime. So as an entry is entered into the database by the Ruby script I want the PHP script to update its mysql_num_row() count in realtime.

I tried using <meta http-equiv="refresh" content="5">, but I don't think this is the best solution.

Does any one have a better solution?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
ethicalhack3r
  • 1,062
  • 3
  • 15
  • 16
  • I adjusted your tags, removing ruby, since it was not really important to the question, and added jquery because it is important in the selected answer. – the Tin Man Nov 21 '10 at 06:07

5 Answers5

2

Use JavaScript on the page to periodically make a call to the server and get some data. Using the jQuery library for cross-browser support of AJAX, you would simply do this:

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

That will make a request to your server every 5 seconds and stick the result of whatever you send back into an element with an id of rowcounter, e.g.

<p>There are <span id='rowcounter'>xx</span> rows in the DB.</p>
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • This looks like the best solution for me. Thank you for your suggestion! – ethicalhack3r Nov 17 '10 at 11:16
  • what do you put in the getrows.php file? does there need to be any html tags with an id of rowcounter or anything? – charlie Sep 27 '13 at 12:03
  • @charlie No: you simply return a text string. No HTML or JSON needed (though you can alternatively do that and jQuery will parse it accordingly). – Phrogz Sep 27 '13 at 13:09
  • will this not affect the performance of the database if all the clients keep making a call to the database to check if the data has been updated every few seconds? – deilkalb Sep 17 '20 at 05:54
1

I would use an ajax updater to keep polling a page that prints your mysql_num_row(). Prototype would be a good solution: http://www.prototypejs.org/api/ajax/updater

wajiw
  • 12,239
  • 17
  • 54
  • 73
0

With only PHP and javascript the only way is to continually check for updates, though wajiw is correct that the ajax route would be less intrusive/noisy than a full page refresh.

It would take an application (or applet) with a standing connection on a port/socket to get updates as they come.

jon_darkstar
  • 16,398
  • 7
  • 29
  • 37
0

If you are going to have large number of visitors, it's better to store your rows number into static file with separate script run by cron (or run your script in endless loop on server). Then show number from this static file with JS, like Phrogz suggested. Otherwise you can exceed mysql connections limit very quickly.

Qwerty
  • 1,732
  • 1
  • 13
  • 18
-2

Polling can never be realtime!

However Juggernaut looks like it meets your requirments: http://juggernaut.rubyforge.org here

But you will need flash on the client side.

James Anderson
  • 27,109
  • 7
  • 50
  • 78