Jump to content

Delete data after a period of time


killuagdt

Recommended Posts

I am using PHP 5.3.2 with Mysql 5.1.47. I am wondering how can I delete MySql's rows after a 60 min? For example I have a table:

Table Clientip

UserIp    Time (default current_timestamp)

When an user go to the website, a row would be add (or update) to the table ClientIp. For example : 10.0.0.8 currrenttime

I have a cronjob to run a script each minutes but I do not know how to check if an user's time value has exceeded 60 min and delete that user's data using mysql_query. So could you please tell me how to do so? Thank you very much for your help.

Link to comment
Share on other sites

  $timeto = 3600;
  $timenw = time();
  $timeout = $timenw - $timeto;
  $exec = mysql_query("DELETE FROM table WHERE recordedtime<'".$timeout."'");

 

Except that MySQL stores CURRENT_TIME and all timestamps as YYYY-MM-DD HH:MM:SS, not a UNIX timestamp.

Link to comment
Share on other sites

Setup a cronjob (in Linux) or scheduled task (in Windows) to run a cleanup script. This is the easiest way.

Many tutorials on connecting to MySQL and running a query.  Then:

 

DELETE FROM `table_name` WHERE (UNIX_TIMESTAMP(`Time`) + 3600) > UNIX_TIMESTAMP()

 

Combine these two.

 

Chances are you don't pay for your server and you don't have access to the cron system.

 

Google something like free cron job system or something like that.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.