如何在 MySQL/PHP 中进行实时数据库轮询?

发布于 2024-10-03 05:29:51 字数 237 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

〃安静 2024-10-10 05:29:51

在页面上使用 JavaScript 定期调用服务器并获取一些数据。使用 jQuery 库来实现 AJAX 的跨浏览器支持,您只需执行以下操作:

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

这将向您的服务器发出请求每 5 秒一次,并将您发送回的任何内容的结果粘贴到 id 为 rowcounter 的元素中,例如

<p>There are <span id='rowcounter'>xx</span> rows in the DB.</p>

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>
蝶…霜飞 2024-10-10 05:29:51

我会使用 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

被你宠の有点坏 2024-10-10 05:29:51

仅使用 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.

巷子口的你 2024-10-10 05:29:51

如果您将有大量访问者,最好将行数存储到静态文件中,并使用 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.

初心未许 2024-10-10 05:29:51

投票永远不可能是实时的!

然而 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文