首页 > 解决方案 > 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);
            }
        }
    }
}

标签: php

解决方案


要删除所有超过 6 小时的记录,您可以使用单个 SQL 操作 - 无需向服务器获取任何内容。我假设您使用的是 MySQL:

$this->conn->query('DELETE FROM live_message WHERE created_at<NOW()- INTERVAL 6 HOUR');

推荐阅读