Would this query work?

Discussion in 'MySQL' started by Tony Brar, Aug 22, 2012.

  1. #1
    Hi guys,

    I want to make a query that, when run, would clear entries more than an hour old.
    It uses some things that are new to me, so I'm not sure if it would work.
    Here is the code:
    
    $sql = "DELETE * FROM tokens WHERE setdate < DATE_SUB(NOW(), INTERVAL 1 HOUR)";
    mysql_unbuffered_query($sql,$con) or die('Error:' . mysql_error . '<br />Error number:' . mysql_errno());
    PHP:
    Also, would that work with this code to set new entries?
    
    $token = mt_rand(1000, 10000);
    $sql="INSERT INTO tokens (token, setdate) VALUES ('$token',now())";
    mysql_unbuffered_query($sql,$con) or die('Error:' . mysql_error . '<br />Error number:' . mysql_errno());
    PHP:
    Thanks,

    - Tony
     
    Solved! View solution.
    Tony Brar, Aug 22, 2012 IP
  2. CafaMagician80

    CafaMagician80 Active Member

    Messages:
    108
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    78
    #2
    I'm using this to select events which are in the future:
    WHERE TIMEDIFF(event.startdate,now()) > 0
    so maybe you could use this to select the ones in the past:
    WHERE TIMEDIFF(event.startdate,now()) < 0
     
    CafaMagician80, Aug 23, 2012 IP
  3. #3
    if setdate - timestamp DB field you can use WHERE setdate < 3600
    if setdate -
    datetime you can use date_dub or another functions. Your query good, don't use field in MySQL function, you need isolate it, like in your query setdate < date_sub... etc. If you use field in MySQL function, index don't be used by this field, if you have it​
     
    afstanislav, Aug 27, 2012 IP
  4. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #4
    Thanks! I just wanted to know if my queries would work.
     
    Tony Brar, Aug 29, 2012 IP