Jump to content

MYSQL- php- Need quick way to delete X day old entries. Ideas?


physaux

Recommended Posts

Hey guys, ok so here is what I want to do.

 

I want to run a cron job every day to run a script that will 'filter' out old entries of my mysql table based on how long ago they were first created. I know how to run the cron job, my problem is with the code on how to do it.

 

-is there a simple, one line way to do this?

-or will I have to make a variable with each row, 'date_created', then calculate which entries are older than X days, and then remove them?

 

Anyone have experience with this kind of thing? I just need an idea, I can code it no problem.

 

Thanks for your help! :)

Link to comment
Share on other sites

Ok so I got around to implementing this but I am having problems. It is not deleting the entries. However, this is because I only have entries from today. I am trying to temporarily get a statment like this:

 

$numdays = 1;
$sqlquery = "DELETE * FROM $table WHERE date_created < date_add(CURRENT_DATE, $numdays Day)";
$result = mysql_query($sqlquery);

 

to delete my entries successfully. But it is not working.

 

**I know my sql query is being processed, becasue I tested a truncate command on $table, and it cleared the data as I wanted.

 

So guys please help me. What am I doing wrong?

 

EDIT: I have tried the following variations of sqlqueries, none of which delete the entries:

//$sqlquery = "DELETE * FROM $table WHERE date_created < date_add(CURRENT_DATE, $numdays Day)";
//$sqlquery = "DELETE * FROM $table WHERE date_created < date_add(curdate(), $numdays Day)";
//$sqlquery = "DELETE * FROM $table WHERE date_created < date_add(CURRENT_DATE, $numdays Days)";
//$sqlquery = "DELETE * FROM $table WHERE date_created < date_add(curdate(), $numdays Days)";

Link to comment
Share on other sites

Ok this is wierd. Here is my entire code:

 

<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);


$numdays = 2;


$DBhost = "XXX";
$DBuser = "XXX";
$DBpass = "XXX";
$DBName = "XXX";
$table = "XXX";


mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
@mysql_select_db("$DBName") or die("Unable to select database $DBName");

//$sqlquery = "delete * from $table where date_created < date_sub(CURRENT_DATE, $numdays Days)";
//$sqlquery = "DELETE * from $table WHERE date_created < DATE_FORMAT( CURRENT_DATE - INTERVAL $numdays DAY, '%Y/%m/%d' ) ";
//$sqlquery = "DELETE * FROM $table WHERE date_created < date_add(CURRENT_DATE, $numdays days)";
$sqlquery = "DELETE * FROM $table";


$result = mysql_query($sqlquery);

mysql_close();

?>

 

Now even the simple one won't work:. "Delete * from $table"! Why is that? Very strange.

But, when I do "TRUNCATE TABLE $table", it DOES WORK. What is wrong?? This makes no sense!!  :'( :'( :'(

Link to comment
Share on other sites

Now even the simple one won't work:. "Delete * from $table"! Why is that? Very strange.

But, when I do "TRUNCATE TABLE $table", it DOES WORK. What is wrong?? This makes no sense!!  :'( :'( :'(

 

Well

Delete * from $table

  is not a valid delete statement.  It would just be

Delete from $table

Link to comment
Share on other sites

Ok I echo'd the value. Here is the output:

 

DELETE FROM splittest WHERE date_created < date_add(CURRENT_DATE, 2 days)

 

Here is the exact code copy pasted so you can see where echo was:

 

$sqlquery = "DELETE FROM $table WHERE date_created < date_add(CURRENT_DATE, $numdays days)";
echo $sqlquery;


$result = mysql_query($sqlquery);

mysql_close();

Link to comment
Share on other sites

Have you tried echo mysql_error(); after your $result = line?

 

Also, you realize that you're doing date_add right? Every date_created is going to be a smaller number than a date in the future (in other words, your script will delete every entry that wasn't created at least $numdays days in the future).

Link to comment
Share on other sites

Yes, I am doing date_add temporarily because I only had recent results, and I wanted to make it delete the records. As you say it should delete them all, but it doesn't. I'm about to add the

 

echo mysql_error();

 

to the code, and see what happens. I'll edit this message when I return

 

EDIT: ok it gave this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2 days)' at line 1

 

any ideas??

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.