PHP:每 10 秒检查一次 mysql 数据库是否有任何新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23192097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:33:43  来源:igfitidea点击:

PHP: Check mysql database every 10 seconds for any new rows

phpmysqlsqlajaxdatabase

提问by blackhawk338

I am making a php chat and am starting the php checking database part. So when a user types something into the chat, it gets recorded in the MySQL database, how would I check the database every 10 seconds so that one user's chat would update with new messages from other users. I know that you can use an ajax request to a page with an interval, but I want the php to be on the same page, instead of having to use numerous pages. This is the code for checking the database

我正在进行 php 聊天,并且正在启动 php 检查数据库部分。因此,当用户在聊天中键入内容时,它会记录在 MySQL 数据库中,我将如何每 10 秒检查一次数据库,以便一个用户的聊天会更新为来自其他用户的新消息。我知道你可以使用一个带有间隔的页面的ajax请求,但我希望php在同一页面上,而不是必须使用多个页面。这是检查数据库的代码

<?php
$con = mysqli_connect('host','user','pass','database');
$query = mysqli_query($con,"SELECT * FROM `messages`");
while ($row=mysqli_fetch_assoc($query)) {
 $user = $row['user'];
 $message = $row['message'];
 echo 'User: ',$user,' Message: ',$message;
}
?>

Thanks in advance anyone!

提前感谢任何人!

采纳答案by hoangvu68

This is what you need. We need set time for ajax auto reload. Don't put everything in one page. Because you must reload page to refresh data. That is bad solution.

这就是你所需要的。我们需要为 ajax 自动重新加载设置时间。不要将所有内容都放在一页中。因为您必须重新加载页面才能刷新数据。那是不好的解决方案。

Call jQuery Ajax Request Each X Minutes

每 X 分钟调用一次 jQuery Ajax 请求

回答by ashutosh

Use MySQL Event Scheduler.

使用 MySQL 事件调度程序。

Below link will guide you through .

下面的链接将引导您完成。

http://www.9lessons.info/2012/10/mysql-event-scheduler.html.

http://www.9lessons.info/2012/10/mysql-event-scheduler.html

I think best option in your case .

我认为在你的情况下最好的选择。

回答by aug

PHP doesn't have a setInterval function. While I'm sure you can use a crontask to automate it on the server, you can also achieve this with some simple Javascript.

PHP 没有 setInterval 函数。虽然我确信您可以使用 crontask 在服务器上自动执行它,但您也可以使用一些简单的 Javascript 来实现这一点。

The concept you are trying to achieve is known as Short Polling. What you want to do is to have a setIntervalfunction in Javascript that constantly makes AJAX requests to your PHP file which performs the check to the database for new messages. Your PHP should return that information to your script where you can then simply populate the user's screen.

您试图实现的概念称为短轮询。您想要做的是setInterval在 Javascript 中拥有一个函数,该函数不断向您的 PHP 文件发出 AJAX 请求,该文件执行对数据库的新消息检查。您的 PHP 应该将该信息返回给您的脚本,然后您可以在其中简单地填充用户的屏幕。

There is also Long Polling where you simply maintain the connection and have a setTimeoutto wait for messages to come in. You can find more information yourself and if you have questions, you can come back here.

还有长轮询,您只需保持连接并setTimeout等待消息进来。您可以自己找到更多信息,如果您有问题,可以回到这里。

A good video about this:

关于这个的好视频:

https://www.youtube.com/watch?v=wHmSqFor1HU

https://www.youtube.com/watch?v=wHmSqFor1HU

Hope this helps.

希望这可以帮助。

回答by haxie

AJAX is probably the simplest solution. You can perform an AJAX request on the same page your PHP code is executing on if you really want to.

AJAX 可能是最简单的解决方案。如果您真的愿意,您可以在执行 PHP 代码的同一页面上执行 AJAX 请求。

(function check() {
    $.get('mypage.php', function(data) {
        doSomethingWith(data);
        setTimeout(check, 5000); // every 5 seconds
    });
})();

回答by Rafayel A.

Make a while for 30 seconds, and check the db every second, once you find a record the while is being broken, also it is being broken when 30 secs are expired.

做一段时间30秒,每秒检查一次数据库,一旦你发现一个记录被打破,当30秒到期时它也会被打破。

$sec = 1;
while($sec <= 30) {
   if(has record)
      Send to the user;
   $sec++;

   sleep(one sec here);
}

Use sleep for 10 secs in order to check every 10 secs...

使用 sleep 10 秒,以便每 10 秒检查一次...