asmith Posted December 2, 2007 Share Posted December 2, 2007 don't know if it is php or sql question : how can i delete a record in sql after a time prioad automaticlly ? for example delete filed1 where id="1" , 5 days after it has been recorded. ? Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 2, 2007 Share Posted December 2, 2007 Add a timestamp field to the records. Record the time each record is created. create a delete routine that checks if timestamp + offset < current timestamp if true, delete you can embed this as a function in any script that gets heavy use on the server, and it should be fairly regular about deletions. If you need precision, you'll need to save the delete routine to its own script and run it via CRON every n number of minutes, hours, or daily. PhREEEk Quote Link to comment Share on other sites More sharing options...
asmith Posted December 2, 2007 Author Share Posted December 2, 2007 what is a delete routine ? sorry but can you please show me an example ?( not too much code, just a simple one) Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 2, 2007 Share Posted December 2, 2007 <?php $offset = '432000'; // 5 days $now = time(); $sql = "DELETE FROM `tableName` WHERE `timeStamp` + $offset < $now "; if ( !$result = mysql_query($sql) ) { // show errors } // all expired records (in this case, 5 days old) were removed PhREEEk Quote Link to comment Share on other sites More sharing options...
Foser Posted December 2, 2007 Share Posted December 2, 2007 would that work even if no one would go to the page? Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 2, 2007 Share Posted December 2, 2007 you can embed this as a function in any script that gets heavy use on the server, and it should be fairly regular about deletions. If you need precision, you'll need to save the delete routine to its own script and run it via CRON every n number of minutes, hours, or daily. Not sure I understand the confusion Foser... PhREEEk Quote Link to comment Share on other sites More sharing options...
monkeybidz Posted December 2, 2007 Share Posted December 2, 2007 If you have a cron file on the side, you may want to add that code to cron file and include it to your index file which is the heaviest visited file and will execute the script. This will only be executed each time the page is visited. Quote Link to comment 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.