15 分钟后删除记录 [英] Delete records after 15 minutes

查看:43
本文介绍了15 分钟后删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不活动的系统.应该在 15 分钟后从非活动表中删除用户.如果在该表中找不到用户,我还有一个代码可以将用户注销.非活动脚本在每次页面刷新时更新用户.

I've got an inactive system. It is supposed to delete users from inactive table after 15 minutes. I have also a code that logs the user out if it cant be found in that table. The inactive script updates user on every page refresh.

这是我尝试过的非活动代码,但到目前为止它不起作用:

This is the inactive code I tried and it doesn't work so far:

$result = mysqli_query($con,"SELECT * FROM inactive");

while($row = mysqli_fetch_array($result))

if ($row['inactive'] > timestampadd(MINUTE, -15, now()))
  {



    }else {

$db_query = "DELETE FROM inactive WHERE username='$username'";
$result = mysql_query($db_query);


    }

推荐答案

CREATE EVENT IF NOT EXISTS `remove_inactives`
ON SCHEDULE EVERY 15 MINUTE
ON COMPLETION PRESERVE
ENABLE
DO
    DELETE FROM `inactive`
        WHERE `timestamp` < DATE_SUB(NOW(), INTERVAL 15 MINUTE)
;

^ 使用集合并忘记重复发生的 MySQL 事件.让服务器为您完成工作:)

^ Use a set and forget recurring MySQL Event. Let the server do the work for you :)

对于 PHP:

mysqli_query($connection, // or mysql_query(
<<<SQL
    CREATE EVENT IF NOT EXISTS `remove_inactives`
    ON SCHEDULE EVERY 15 MINUTE
    ON COMPLETION PRESERVE
    ENABLE
    DO
        DELETE FROM `inactive`
            WHERE `timestamp` < DATE_SUB(NOW(), INTERVAL 15 MINUTE)
    ;
SQL;
); // ends *_query() call

您只需要运行一次即可在服务器上安装!

这篇关于15 分钟后删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆