0

Okay.

So i have a table that's being used on my website using PHP to pull out the contents.

I need it to wipe the table every day at 4am. How do i do this?

Structure CREATE TABLE IF NOT EXISTS chat ( id int(255) NOT NULL AUTO_INCREMENT, message longtext NOT NULL, by_user longtext NOT NULL, is_admin longtext NOT NULL, color longtext NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=144 ;

2 Answers2

1

If you are on linux or something similar, you can use a cron job.

type the following at the command line. This will result in your cron file being open in an editor (which we are setting to be vi)

export EDITOR=vi ;
crontab -e

Type 'i' to enter insert mode and copy paste the following into it.

* 4 * * *  /path/to/mysql database -e 'TRUNCATE chat' > /dev/null/ 2>&1

then type ESC :wq to save the file. Now you have a cron job that clears up the table at 400 am each day.

The last part redirects output to /dev/null If you have a specific mysql username and password to you the command becomes

/path/to/mysql -u username -p password database -e 'TRUNCATE chat'

e4c5
  • 52,766
  • 11
  • 101
  • 134
0

Use the SQL statement TRUNCATE TABLE chat; to clear out the data from the table.

As for having the command get executed at 4AM every morning, this will have to be done using some kind of scheduling component like cron or Windows Task Scheduler. You could create a PHP script that executes the statement above, and then invoke the script on the schedule. Make sure not to place this script in a location that can be activated by your website, otherwise users may be able to invoke it at any time during the day.

joncloud
  • 757
  • 5
  • 14