sonny Posted July 20, 2009 Share Posted July 20, 2009 Hi Does anyone know the code for how to delete a old mysql entry older then X amount of seconds (2400 etc using a php page Sonny I have a table named "visits" and a unix time stamp field named "time" Quote Link to comment https://forums.phpfreaks.com/topic/166636-solved-removing-old-mysql-data-with-php-page/ Share on other sites More sharing options...
rhodesa Posted July 20, 2009 Share Posted July 20, 2009 $before = time() - 2400; $sql = "DELETE FROM `visits` WHERE `time`< '$before'"; Quote Link to comment https://forums.phpfreaks.com/topic/166636-solved-removing-old-mysql-data-with-php-page/#findComment-878690 Share on other sites More sharing options...
sonny Posted July 20, 2009 Author Share Posted July 20, 2009 That does not delete anything, even if I set it for 1 second Here is what the database looks like CREATE TABLE `visits` ( `id` int(11) NOT NULL auto_increment, `ip` varchar(20) NOT NULL default '', `country` varchar(50) NOT NULL default '', `region` varchar(50) NOT NULL default '', `city` varchar(50) NOT NULL default '', `time` int(11) NOT NULL default '0', `vcode` int(11) NOT NULL default '0', PRIMARY KEY (`id`) ); This is what the time field looks like (unix time)-->1248106995 this what my php looks like I tested with <?php require "./connect.php"; $before = time() - 5; $sql = "DELETE FROM `visits` WHERE `time`< '$before'"; ?> I would like to delete entry's older then x amount of seconds to test first Thanks for helping Sonny Quote Link to comment https://forums.phpfreaks.com/topic/166636-solved-removing-old-mysql-data-with-php-page/#findComment-878755 Share on other sites More sharing options...
rhodesa Posted July 20, 2009 Share Posted July 20, 2009 oh...well...you need to add a mysql_query() to it...i just assumed you knew to do that: <?php require "./connect.php"; $before = time() - 5; $sql = "DELETE FROM `visits` WHERE `time` < '$before'"; mysql_query($sql) or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/166636-solved-removing-old-mysql-data-with-php-page/#findComment-878758 Share on other sites More sharing options...
sonny Posted July 20, 2009 Author Share Posted July 20, 2009 Thank you so much! it worked Would it be hard to covert that to 1 day if ever needed? or do I just add up the seconds. again thanks Sonny Quote Link to comment https://forums.phpfreaks.com/topic/166636-solved-removing-old-mysql-data-with-php-page/#findComment-878769 Share on other sites More sharing options...
rhodesa Posted July 20, 2009 Share Posted July 20, 2009 you can add up the seconds or use the strtotime() function..it's pretty versatile: $before = strtotime('-1 day'); Quote Link to comment https://forums.phpfreaks.com/topic/166636-solved-removing-old-mysql-data-with-php-page/#findComment-878770 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.