php - PHP 6 小时后删除记录
问题描述
请我尝试在数据库中删除 6 小时后的记录,但所有努力都失败了。下面是功能,我是新手请。
function chektime()
{
$sql = "SELECT * FROM live_message";
$row = array();
$sql2 = $this->conn->query($sql);
if ($sql2->num_rows > 0) {
while ($res = $sql2->fetch_assoc()) {
$row[] = $res;
foreach ($row as $rows) {
$id = $rows['id_live_message'];
$timers = $rows['created_at'];
$newtime = strtotime($timers);
$t = time();
$timediff = $newtime - $t;
$neetousetime = $timediff / 3600 % 7;
}
if ($neetousetime >= 4) {
$timedetect = "DELETE FROM live_message WHERE id_live_message ='$id' ";
$sql = $this->conn->query($timedetect);
}
}
}
}
解决方案
要删除所有超过 6 小时的记录,您可以使用单个 SQL 操作 - 无需向服务器获取任何内容。我假设您使用的是 MySQL:
$this->conn->query('DELETE FROM live_message WHERE created_at<NOW()- INTERVAL 6 HOUR');
推荐阅读
- kubernetes - 使用 AWS Container Insights 监控 Kubernetes CronJob 完成情况
- php - LARAVEL 得到错误 500 | SmarterASP.NET 托管服务器中的服务器错误
- c++ - 如何将双精度值直接转换为 PQ^-1 模 MOD,其中 q 与 MOD 互质
- perforce - 检查给定的 perforce 标签是否存在
- python - 检索对象
- excel - 使用 VBA 在 Google 表单中选择下拉列表选项
- php - 如何将图像路径存储到表中?
- flutter - 颤振错误:未处理的异常:将 RCPT 发送到:<> 后,响应没有以任何开头:[2]
- python-3.x - 在 DataFrame 中选择元素
- angular - 单击组件 A 功能我想使用服务打印组件 B 中的一些数据