Php/Msql

WarriorH

New Member
Messages
55
Reaction score
0
Points
0
Is it possible to create a code that will empty a database every 20 minutes? If so, could you please explain?


-Thanks!
 

taekwondokid42

New Member
Messages
268
Reaction score
0
Points
0
yes.

You would start with a script saying DELETE FROM db all tables

Then, you would set up a cron job that would run the script every 20 min.
 

WarriorH

New Member
Messages
55
Reaction score
0
Points
0
Is this correct?

For the script:

<?php
$sql=mysql_connect('*******','*******','*******') or die
("Error: Couldn't connect to database!");
mysql_select_db('*******) or die ("Error: Couldn't connect to database!");




mysql_query("DELETE FROM data")
or die(mysql_error());
?>


And for the cron job, I know how to make it run every 20 mins, but what is the command to run?
 
Last edited:

taekwondokid42

New Member
Messages
268
Reaction score
0
Points
0
$tables = mysql_query("SHOW TABLES LIKE '\_%'");

while ($table = mysql_fetch_array($tables)) {
$table = current($table);
mysql_query("DROP TABLE $table")or die(mysql_error());
}


That should do it for clearing the database. Just realize, that this script will truly clear your whole database.

As for the cron job:

/usr/local/bin/php -q ./public_html/agelesscommanders/foldername/filename.php

That is the command you would use. I'm not fully sure what numbers you would put in to get 20 min though. My script (0 0 * * *) runs at midnight, every night.

I hope that I was of help.
 

WarriorH

New Member
Messages
55
Reaction score
0
Points
0
You confused me a bit- should i replace $tables with my info? Can you give me like comments for each line, im new to cron jobs.
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
WarriorH, with taekwandokid42's script, you'll lose your database structure too. (Not only data, but also your tables will be gone.)

You'll need this for your cron job:
3 seperate jobs, all executing the same command. (CPanel - cron jobs - standard)
1: Minutes: 0, Hours: every hour, Days: every day, Month: every month, Weekdays: every weekday.
2: same as 1, but Minutes: 20
3: same as 1, but Minutes: 40

Edit: it could be a lot simpler too...
CPanel - cron jobs - advanced:
0,20,40 * * * *
 
Last edited:

woiwky

New Member
Messages
390
Reaction score
0
Points
0
If you change DROP to TRUNCATE, you'll clear all data and maintain table structure.
 

t2t2t

New Member
Messages
690
Reaction score
0
Points
0
You'll need this for your cron job:
3 seperate jobs, all executing the same command. (CPanel - cron jobs - standard)
1: Minutes: 0, Hours: every hour, Days: every day, Month: every month, Weekdays: every weekday.
2: same as 1, but Minutes: 20
3: same as 1, but Minutes: 40

Edit: it could be a lot simpler too...
CPanel - cron jobs - advanced:
0,20,40 * * * *
... or just in basic mode use CTRL+click to choose 0, 20 and 40 minutes from the list.
 
Top