MYSQL question

Message boards : Number crunching : MYSQL question
Message board moderation

To post messages, you must log in.

AuthorMessage
Profile Mr. Majestic
Volunteer tester
Avatar

Send message
Joined: 26 Nov 07
Posts: 4752
Credit: 258,845
RAC: 0
United States
Message 739190 - Posted: 15 Apr 2008, 0:51:50 UTC

Is there a way to make a PHP script that will delete information from a MYSQL database after a certain amount of time?

ID: 739190 · Report as offensive
Scarecrow

Send message
Joined: 15 Jul 00
Posts: 4520
Credit: 486,601
RAC: 0
United States
Message 739214 - Posted: 15 Apr 2008, 1:53:38 UTC - in response to Message 739190.  

Is there a way to make a PHP script that will delete information from a MYSQL database after a certain amount of time?


It can only be done on days that end in "y".....

Sumpin' like this?

<?php

//Connect to the Database Server
$con = mysql_connect("myhost","myuser","mypassword");
if (!$con)
  {
      die('Could not connect: ' . mysql_error());
            }

// Select Database to use            
 mysql_select_db("mydatabase", $con);

// $now is the current "unix time"
$now=time();

// Subtract 86400 from $now to get what the unix time was 24 hours ago.
// (there are 3600 "unix ticks" per hour, thus 3600*24=86400 in a day)
$TimeToDelete=$now-86400;

// Do the query, deleting anything older than $TimeToDelete (24 hours)
 mysql_query("DELETE FROM mytable WHERE created < $TimeToDelete");

// Close the connection                              
mysql_close($con);

?>


This assumes that the database "mydatabase" has a table named "mytable" and that table has a column named "created" that contains the unix time stamp for when that row was inserted.
ID: 739214 · Report as offensive
Profile Mr. Majestic
Volunteer tester
Avatar

Send message
Joined: 26 Nov 07
Posts: 4752
Credit: 258,845
RAC: 0
United States
Message 739235 - Posted: 15 Apr 2008, 2:39:56 UTC - in response to Message 739214.  

Is there a way to make a PHP script that will delete information from a MYSQL database after a certain amount of time?


It can only be done on days that end in "y".....

Sumpin' like this?

<?php

//Connect to the Database Server
$con = mysql_connect("myhost","myuser","mypassword");
if (!$con)
  {
      die('Could not connect: ' . mysql_error());
            }

// Select Database to use            
 mysql_select_db("mydatabase", $con);

// $now is the current "unix time"
$now=time();

// Subtract 86400 from $now to get what the unix time was 24 hours ago.
// (there are 3600 "unix ticks" per hour, thus 3600*24=86400 in a day)
$TimeToDelete=$now-86400;

// Do the query, deleting anything older than $TimeToDelete (24 hours)
 mysql_query("DELETE FROM mytable WHERE created < $TimeToDelete");

// Close the connection                              
mysql_close($con);

?>


This assumes that the database "mydatabase" has a table named "mytable" and that table has a column named "created" that contains the unix time stamp for when that row was inserted.

Thanks scare crow! I will give it a try tomorrow as I am too tired to do it right now.

ID: 739235 · Report as offensive
Profile RandyC
Avatar

Send message
Joined: 20 Oct 99
Posts: 714
Credit: 1,704,345
RAC: 0
United States
Message 739564 - Posted: 15 Apr 2008, 23:15:31 UTC - in response to Message 739214.  

Is there a way to make a PHP script that will delete information from a MYSQL database after a certain amount of time?


It can only be done on days that end in "y".....

Bummer. Guess that rules out working on sundae. Well, that's a treat anyway.

<ducks and runs for cover>
ID: 739564 · Report as offensive
Profile Toby
Volunteer tester
Avatar

Send message
Joined: 26 Oct 00
Posts: 1005
Credit: 6,366,949
RAC: 0
United States
Message 739587 - Posted: 15 Apr 2008, 23:56:12 UTC
Last modified: 15 Apr 2008, 23:56:48 UTC

No need to involve php in the date computations...

This will work if your column is a date, timestamp or datetime type:
DELETE FROM table WHERE created < NOW() - INTERVAL 7 DAY


For an int column with a unix timestamp in it:
DELETE FROM table WHERE created < UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)


More about dates in mysql: Mysql manual page on date and time functions
A member of The Knights Who Say NI!
For rankings, history graphs and more, check out:
My BOINC stats site
ID: 739587 · Report as offensive
Profile Mr. Majestic
Volunteer tester
Avatar

Send message
Joined: 26 Nov 07
Posts: 4752
Credit: 258,845
RAC: 0
United States
Message 739604 - Posted: 16 Apr 2008, 0:21:51 UTC - in response to Message 739587.  

No need to involve php in the date computations...

This will work if your column is a date, timestamp or datetime type:
DELETE FROM table WHERE created < NOW() - INTERVAL 7 DAY


For an int column with a unix timestamp in it:
DELETE FROM table WHERE created < UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)


More about dates in mysql: Mysql manual page on date and time functions

The idea was to make it work automatically so I would only have to intervene if something went wrong.

ID: 739604 · Report as offensive
Profile Toby
Volunteer tester
Avatar

Send message
Joined: 26 Oct 00
Posts: 1005
Credit: 6,366,949
RAC: 0
United States
Message 739674 - Posted: 16 Apr 2008, 4:12:09 UTC

Well you can still put my queries inside of a php/perl/java/shell script. I just prefer doing things in one place. So instead of spreading the logic between the php script and the SQL I just put it all in the SQL. Either way will work fine.
A member of The Knights Who Say NI!
For rankings, history graphs and more, check out:
My BOINC stats site
ID: 739674 · Report as offensive
Profile AlphaLaser
Volunteer tester

Send message
Joined: 6 Jul 03
Posts: 262
Credit: 4,430,487
RAC: 0
United States
Message 739838 - Posted: 16 Apr 2008, 15:22:44 UTC


Well you can still put my queries inside of a php/perl/java/shell script. I just prefer doing things in one place. So instead of spreading the logic between the php script and the SQL I just put it all in the SQL. Either way will work fine.


Additionally, keeping more of the logic in the SQL statement also allows the SQL engine to optimize the query in some cases.

If this is on a *nix machine you may want to look into setting up a cron script if you want the script to run periodically.


ID: 739838 · Report as offensive

Message boards : Number crunching : MYSQL question


 
©2024 University of California
 
SETI@home and Astropulse are funded by grants from the National Science Foundation, NASA, and donations from SETI@home volunteers. AstroPulse is funded in part by the NSF through grant AST-0307956.