如何在 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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在页面上使用 JavaScript 定期调用服务器并获取一些数据。使用 jQuery 库来实现 AJAX 的跨浏览器支持,您只需执行以下操作:
这将向您的服务器发出请求每 5 秒一次,并将您发送回的任何内容的结果粘贴到 id 为
rowcounter
的元素中,例如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:
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.我会使用 ajax 更新程序来不断轮询打印 mysql_num_row() 的页面。原型将是一个很好的解决方案: http://www.prototypejs.org/api/ajax/updater< /a>
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
仅使用 PHP 和 javascript,唯一的方法是不断检查更新,尽管 wajiw 是正确的,ajax 路由比全页面刷新的干扰/噪音要小。
需要一个在端口/套接字上具有常设连接的应用程序(或小程序)来获取更新。
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.
如果您将有大量访问者,最好将行数存储到静态文件中,并使用 cron 运行单独的脚本(或在服务器上无限循环运行脚本)。然后用 JS 显示这个静态文件中的数字,就像 Phrogz 所建议的那样。否则你很快就会超过 mysql 连接限制。
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.
投票永远不可能是实时的!
然而 Juggernaut 看起来满足您的要求:
http://juggernaut.rubyforge.org 此处
但是您需要在客户端安装 Flash。
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.