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? Link to comment https://forums.phpfreaks.com/topic/119710-solved-removing-old-data/ 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? Link to comment https://forums.phpfreaks.com/topic/119710-solved-removing-old-data/#findComment-616751 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.. Link to comment https://forums.phpfreaks.com/topic/119710-solved-removing-old-data/#findComment-616753 Share on other sites More sharing options...
monkeypaw201 Posted August 14, 2008 Author Share Posted August 14, 2008 bump Link to comment https://forums.phpfreaks.com/topic/119710-solved-removing-old-data/#findComment-616910 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. Link to comment https://forums.phpfreaks.com/topic/119710-solved-removing-old-data/#findComment-616913 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.