PHP:每10秒检查一次mysql数据库的新行 [英] PHP: Check mysql database every 10 seconds for any new rows
问题描述
我正在做一个php聊天,并开始php检查数据库部分。因此,当用户在聊天中键入内容时,它会记录在MySQL数据库中,我如何每10秒检查一次数据库,以便一个用户的聊天将更新来自其他用户的新消息。我知道你可以使用ajax请求到一个页面有一个间隔,但我想要php在同一个页面,而不必使用大量的页面。这是检查数据库的代码
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
$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;
}
?>
先感谢任何人!
推荐答案
这是你需要的。我们需要为ajax自动重新加载设置时间。不要把一切都放在一页。因为您必须重新加载页面才能刷新数据。这是坏的解决方案。
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.
这篇关于PHP:每10秒检查一次mysql数据库的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!