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

↘锁芯ラ 提交于 2019-12-02 19:52:09

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!