andrew_biggart Posted February 20, 2009 Share Posted February 20, 2009 I was just wondering is there any way of clearing entrys from your database after say a week? so that it doesnt get clogged up? Quote Link to comment https://forums.phpfreaks.com/topic/146155-solved-clearing-entrys-from-your-database/ Share on other sites More sharing options...
premiso Posted February 20, 2009 Share Posted February 20, 2009 Yep, using a Cron Job if on Linux or Scheduled Tasks if on Windows. Given that the table you want to clear data from has a Datetime field this is entirely possible. If it does not you should look into adding one. Quote Link to comment https://forums.phpfreaks.com/topic/146155-solved-clearing-entrys-from-your-database/#findComment-767314 Share on other sites More sharing options...
andrew_biggart Posted February 20, 2009 Author Share Posted February 20, 2009 im confused as to what u mean mate.... I have a date and time field yea so that makes it easier but how do i actually do it? is it via php code or what? Quote Link to comment https://forums.phpfreaks.com/topic/146155-solved-clearing-entrys-from-your-database/#findComment-767348 Share on other sites More sharing options...
premiso Posted February 20, 2009 Share Posted February 20, 2009 Cron Job using PHP CLI (Command Line Interface). Basically the cron job is set to run every x minutes or hours or days etc. (Google Cron Job to read more up on that). What a cron job does (or scheduled task) is automatically run some code at the times specified. An example of a line for a script that runs every day in a cron job would be: 0 0 * * * /usr/bin/php -f /home/username/deleteDaily.php Then the code for your script to erase the entries would be something like this: deleteDaily.php <?php // mysql connection info mysql_query("DELETE FROM table_name WHERE datecol > " . strtotime('+1 week')); ?> This "should" (test it without deleting before using) delete any entry that is > 1 week old. I do not know how MySQL different date fields handle a unix timestamp, so that may or may not work. But that is the basic gist, for a cron job running via PHP CLI check out this Tutorial. For how to do this on a windows server, look into Scheduled tasks. Quote Link to comment https://forums.phpfreaks.com/topic/146155-solved-clearing-entrys-from-your-database/#findComment-767354 Share on other sites More sharing options...
angelcool Posted February 20, 2009 Share Posted February 20, 2009 You can do it via PHP code or through a Cron Job (http://en.wikipedia.org/wiki/Cron), which is a method to automatically run scripts every x period of time. If you want to use PHP for that you will have to create a function and run it every time you modify your database (add record,update record). Do a google search for deleting mysql records older than. Hope this helps. Angel Quote Link to comment https://forums.phpfreaks.com/topic/146155-solved-clearing-entrys-from-your-database/#findComment-767356 Share on other sites More sharing options...
andrew_biggart Posted February 20, 2009 Author Share Posted February 20, 2009 Thanks everyone <3 Quote Link to comment https://forums.phpfreaks.com/topic/146155-solved-clearing-entrys-from-your-database/#findComment-767387 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.