Deleting records older than 2 days?

Discussion in 'PHP' started by mokimofiki, Jun 5, 2009.

  1. #1
    I am trying to delete any comments that are saved in the comments table older than 2 days. I have a cron job that will complete the action.

    $date = date("Y-m-d", strtotime("-2 day"));

    $resultupdate3 = mysql_query("SELECT * FROM comments") or die(mysql_error());

    while($rowupdate3 = mysql_fetch_array($resultupdate3))
    {
    mysql_query("DELETE FROM comments WHERE date = '$date'");
    }

    although the code above does not seem to do what I require and i'm not sure what is wrong.

    echo "$date"; // will display the date 2 days prior to today in the correct format although my query will not delete the records.

    Any thoughts?
     
    mokimofiki, Jun 5, 2009 IP
  2. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Instead of the resultupdate3 and while commands, you can try only running

    mysql_query("DELETE FROM comments WHERE date='$date'");
     
    SHOwnsYou, Jun 5, 2009 IP
    mokimofiki likes this.
  3. HorseGalleria

    HorseGalleria Peon

    Messages:
    91
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    HorseGalleria, Jun 5, 2009 IP
  4. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #4
    Thank you it works great now
     
    mokimofiki, Jun 8, 2009 IP