monkeypaw201 Posted August 14, 2008 Share Posted August 14, 2008 So, I have a database and I would like to "flush" it every 15 minutes and remove any data older than 15 minutes. The code I am using right now removes everything even If its just a few minutes old. The column `last_update` is formatted using time() <?php #Connect to database $con = mysql_connect("localhost","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } #Select database mysql_select_db("database", $con); #Declare Flush Timestamp $timestamp = time(); #Cycle Through and remove excess flights $flush = mysql_query("SELECT * FROM `active_flights`"); while($row_flush = mysql_fetch_array($flush)) { #Generate Timeout Timestamp $timeflush = $row_flush['last_update'] + 900; if($timeflush > $timestamp) { mysql_query("DELETE FROM `active_flights` WHERE `id` = '$row_flush[id]' LIMIT 1"); mysql_query("DELETE FROM `active_flights_archive` WHERE `archive_id` = '$row_flush[archive_id]'"); } } Any suggestions? Quote Link to comment Share on other sites More sharing options...
adam84 Posted August 14, 2008 Share Posted August 14, 2008 Couldn't you use a cron job to run that script every 15 minutes? Quote Link to comment Share on other sites More sharing options...
monkeypaw201 Posted August 14, 2008 Author Share Posted August 14, 2008 Couldn't you use a cron job to run that script every 15 minutes? Yes, It is a cron job... but the problem is that right now when it runs, it deletes EVERYTHING, not just old data.. Quote Link to comment Share on other sites More sharing options...
monkeypaw201 Posted August 14, 2008 Author Share Posted August 14, 2008 bump Quote Link to comment Share on other sites More sharing options...
revraz Posted August 14, 2008 Share Posted August 14, 2008 Eliminate the middleman and only SELECT the rows that fall into that criteria instead of reading all of them in and then checking. 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.