PHP + MySql Cron Job,删除超过1小时的行 [英] PHP + MySql Cron Job, remove rows that are over 1 hour old

查看:171
本文介绍了PHP + MySql Cron Job,删除超过1小时的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Sql查询从1小时前选择?

我当前的cron作业脚本从数据库中删除所有行是不好的,如果用户想要重置密码时cronjob运行时,我可以检查mysql当行添加到数据库,这样只删除行已超过1小时?

my current cron job script removes all rows from database which is not good if user wants to reset password at that time when cronjob runs, how I can check with mysql when row is added to database and that way remove only rows that are been there more than 1 hour?

我的cron_reset.php

My cron_reset.php

<?php
require_once('mysql_config.php');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) 
{
    die('Failed to connect to server: ' . mysql_error());
}

$db = mysql_select_db(DB_DATABASE);
if(!$db) 
{
    die("Unable to select database");
}


function clean($str) 
{
    $str = @trim($str);
    if(get_magic_quotes_gpc()) 
    {
        $str = stripslashes($str);
    }
    return mysql_real_escape_string($str);
}

$password = clean($_GET['password']);
if($password != $cron_password)
{
    die('Access Denied');
}
else
{
    $qry = "DELETE FROM cron_reset";
    $result = mysql_query($qry);
    if($result) 
    {
        die('Success');
    }
    else 
    {
        die(mysql_error());
    }
}
?>

这是将电子邮件+重置代码添加到数据库的行

This is that line which adds email + reset code to database

            $qry = "INSERT INTO cron_reset(email, code) VALUES('$email','$code')";
            $result = @mysql_query($qry);


推荐答案

在数据库模式中,新列,用于存储需求时间(例如,ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP),并在查询中添加where子句(WHERE ts

In your DB schema, you will have to add a new column to store the time of the demand (eg ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) and add a where clause in your query (WHERE ts < NOW()- INTERVAL 1 HOUR)

这篇关于PHP + MySql Cron Job,删除超过1小时的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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