如何检测一行是否在 x 秒前插入到数据库中 [英] How can I detect if a row was inserted into the database x seconds ago

查看:43
本文介绍了如何检测一行是否在 x 秒前插入到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在开发垃圾邮件检测系统,我想检测是否在 x 秒前插入了一行.x 可能是 5 秒,所以我想看看在不到 5 秒前是否有一行插入到表中.

Basically, I'm working on a spam detection system and I would like to detect if a row was inserted less the x seconds ago. x could be 5 second, so I would want to see if a row was inserted into the table less then 5 seconds ago.

用于示例目的的表名可以是 my_table.这是我想出来的……

The table name for example purposes could be my_table. This is what I came up with...

$spam_validate = mysql_query("select * FROM my_table WHERE date_time < '3'");

那我就....

    if (mysql_num_rows($spam_validate)==0) {
    //keep going
    } else {
    echo 'Spam was detected';
    }

如果你们中的任何人能在这里指出我正确的方向,我显然是不正确的,所以非常感谢任何帮助.

If any of you could point me into the right direction here, I'm obviously not correct so any help is much appreciated.

推荐答案

您需要一个时间戳字段(可以是 DATETIME) 在该表中,您在插入行时设置.

You need a timestamp field (can be of type DATETIME) in that table, which you set when you insert the row.

INSERT INTO foo (spam, eggs, timestamp) VALUES (42, 88, NOW())

然后你可以选择反对它.这个查询应该可以工作.

Then you can select against it. This query should work.

SELECT * FROM foo WHERE timestamp BETWEEN (NOW() - INTERVAL 5 SECOND) AND NOW()

注意 DATETIME 算法,它类似于使用 DATEADD 直接.

Note the DATETIME arithmetic, which is similar to using DATEADD directly.

这篇关于如何检测一行是否在 x 秒前插入到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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