开发者

How do I do realtime database polling in MySQL/PHP?

I have a Ruby script which is constantly updating a MySQL database. I want to show 开发者_如何学Pythonthe "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?


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>


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜