gpt4 book ai didi

php - 提取最近两个小时的消息?

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:46 24 4
gpt4 key购买 nike

我做了一个简单的聊天系统。我只想从数据库中提取我们的最后两条消息而不是完整的数据库。有人可以告诉我如何做到这一点吗?谢谢!

代码:

    if  (isset($_SESSION['id'])) {
$userId = $_SESSION['id'];
$username = $_SESSION['username'];
$userLabel = $_SESSION['nickname'];
}

$connect = mysqli_connect("", "", "", "root");
mysqli_select_db($connect, "root");
$result1 = mysqli_query($connect, "SELECT * FROM chat ORDER by id DESC");

while ($extract = mysqli_fetch_assoc($result1)) {
if ($extract['name'] == 'admin1') {
$color = '#D00003';
}
else if ($extract['name'] == 'admin2') {
$color = '#7E0002';
}
else {
$color = '#FFFFFF';
}

if ($extract['name'] == $userLabel) {
echo "
<div class='usermessage'>
<div class='mheader'>
<h2 style='color:".$color."'>" . $extract['time'] . "</h2>
<h3 style='color:".$color."'>" . $extract['name'] . "</h3>
</div>
<p>" . $extract['message'] . "</p>
</div>";
}
else {
echo "
<div class='message'>
<div class='mheader'>
<h2 style='color:".$color."'>" . $extract['time'] . "</h2>
<h3 style='color:".$color."'>" . $extract['name'] . "</h3>
</div>
<p>" . $extract['message'] . "</p>
</div>";
}
}
?>

最佳答案

你应该使用 SQL date_sub像那样:

SELECT *
FROM `chat`
WHERE `time` >= DATE_SUB(NOW(), INTERVAL 2 HOUR)
ORDER by `id` DESC

关于php - 提取最近两个小时的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33670185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com