delete SQL data .. expiration date?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

delete SQL data .. expiration date?

Post by jramaro »

Hi
i would like to delete SQL data after two hours.
I've not done that before , so im wondering how this works in the database.
I currently have TIMESTAMP as a database field.

Thank you
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

$sql = "DELETE FROM mytable WHERE time<'$time_two_hours_ago'";
Check the data/time functions in the manual for calculating the time two hours ago -- or see the database dogs to do it in SQL.
(#10850)
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

Post by jramaro »

That code snippet looks like a a command made from a webpage/ script

will that work for data already in a database?
because i want the data in a database to expire automatically after two hours.
Without having to make any query to it manually.


Thank you
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

WHERE DATE_SUB(CURDATE(), INTERVAL 24 HOUR)) <= time;
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

jramaro wrote:That code snippet looks like a a command made from a webpage/ script

will that work for data already in a database?
because i want the data in a database to expire automatically after two hours.
Without having to make any query to it manually.
You posted this in PHP Code so I assumed that you wanted a PHP solution. What database are you using?
(#10850)
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

Post by jramaro »

database type: MySQL database
I posted this in PHP code, because i didnt know if the command for expiration has to start with PHP on the insert SQL?

---

That snip by Jcart seems to look like what I'm tryin to find.

on DATE_SUB .. should this be a column name , or a command in SQL similar to TIMESTAMP?


Thank you
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

Jcart = MySQL Dictionary
Post Reply