如何在MySQL/PHP中进行实时数据库轮询? [英] How do I do realtime database polling in MySQL/PHP?
问题描述
我有一个Ruby脚本,该脚本正在不断更新MySQL数据库.我想实时显示"mysql_num_rows()
".因此,当通过Ruby脚本将条目输入数据库时,我希望PHP脚本实时更新其mysql_num_row()计数.
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.
我尝试使用<meta http-equiv="refresh" content="5">
,但是我认为这不是最好的解决方案.
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?
推荐答案
在页面上使用JavaScript定期调用服务器并获取一些数据.使用 jQuery 库获得对AJAX的跨浏览器支持,您只需执行以下操作:
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
});
这将每5秒向您的服务器发送一次请求,并将发送回的结果粘贴到ID为rowcounter
的元素中,例如
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>
这篇关于如何在MySQL/PHP中进行实时数据库轮询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!