Forum Moderators: coopster

Message Too Old, No Replies

automatically deleting old records?

trying to delete old database entries

         

distorto

6:57 pm on Jul 24, 2007 (gmt 0)

10+ Year Member



I'm sort of new to working with databases...I'm trying to write a script that automatically deletes old database entries. I thought this should work, but I keep throwing syntax errors. any ideas?

CREATE EVENT delete_old_mess ON SCHEDULE EVERY1 HOUR DO DELETE FROM 'messages' WHERE 'date' < NOW( ) - INTERVAL 2 DAY

distorto

6:59 pm on Jul 24, 2007 (gmt 0)

10+ Year Member



sorry - forgot to mention - 'messages' is a table on a database called 'users' with a collumn called 'date'.
the table is fully populated with messages.

distorto

7:23 pm on Jul 24, 2007 (gmt 0)

10+ Year Member



sorry for the backasswards post...
I'm realizing that maybe the problem lies in my naive assumption that the timestamp info in the date collumn is going to be understood by mysql. The 'date' collumn in the aforementioned table 'messages' contains timestamps. Since I'm comparing the dates in the collumn to mysql's internal date action, do I need to do something differently to get mysql to compare the dates?

PHP_Chimp

7:27 pm on Jul 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So are you using a php script to remove the old records or are you using a database event to remove the old records?
As you have posted some db code, so if you are using php then you may well need to post some of that code so we can see how it all goes together.

If you are using php then what event are you using to triger this clean up? Is it something that happens every time someone accesses the db, or do you have a script that is always running in the background and just has a time delay built into the code?

distorto

7:34 pm on Jul 24, 2007 (gmt 0)

10+ Year Member



sorry. this is maybe more of an sql/database question. It is indeed a database event that deletes the old records.

PHP_Chimp

7:38 pm on Jul 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would ask in the db area, you should get an answer faster.

WesleyC

9:30 pm on Jul 24, 2007 (gmt 0)

10+ Year Member



One thing you might try is to do a "SELECT NOW() FROM messages" and see what exactly NOW() is giving you.

distorto

9:34 pm on Jul 24, 2007 (gmt 0)

10+ Year Member



I found the problem. EVENT is an addition to mysql 5.1 and above. so it's still beta...which means I can't use it.
What else is out there for automatic archiving?
I want to delete messages over a month old every night or something like that.

WesleyC

4:46 pm on Jul 25, 2007 (gmt 0)

10+ Year Member



Depending on the server OS, you could set up a cron job or a windows timed event (don't know the name of it). These are an event that occurs at a specific time on the server, and may be used to run a .php file via the command line (that's the simplest way). In that PHP file, run your DELETE query.

distorto

4:58 pm on Jul 25, 2007 (gmt 0)

10+ Year Member



thanks for the reply. I did wind up using a cron job to run a php script that does what I need every day. seems like it will work fine.