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?
Instead of the resultupdate3 and while commands, you can try only running mysql_query("DELETE FROM comments WHERE date='$date'");
You can do it with one MySQL statement using DATE and INTERVAL. http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html