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

时间:2022-10-13 13:28:34

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!

谢谢提前任何人!

5 个解决方案

#1


0  

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请求

#2


3  

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 .

我认为这是你的最佳选择。

#3


1  

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秒,每秒钟检查一次db,一旦你发现一个记录,同时被破坏了,当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...

使用睡眠10秒,以检查每10秒……

#4


0  

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
    });
})();

#5


0  

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 setInterval function 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.

您试图实现的概念是短轮询。您需要做的是在Javascript中使用setInterval函数,该函数不断地向PHP文件发出AJAX请求,PHP文件执行对数据库的新消息检查。PHP应该将这些信息返回到脚本中,然后只需填充用户的屏幕。

There is also Long Polling where you simply maintain the connection and have a setTimeout to 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.

希望这个有帮助。

#1


0  

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请求

#2


3  

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 .

我认为这是你的最佳选择。

#3


1  

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秒,每秒钟检查一次db,一旦你发现一个记录,同时被破坏了,当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...

使用睡眠10秒,以检查每10秒……

#4


0  

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
    });
})();

#5


0  

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 setInterval function 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.

您试图实现的概念是短轮询。您需要做的是在Javascript中使用setInterval函数,该函数不断地向PHP文件发出AJAX请求,PHP文件执行对数据库的新消息检查。PHP应该将这些信息返回到脚本中,然后只需填充用户的屏幕。

There is also Long Polling where you simply maintain the connection and have a setTimeout to 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.

希望这个有帮助。