Jump to content

Cron Job


Collegeboox

Recommended Posts

Does anyone know how to write the php for a cron job (delete date from a db) this is what I have but it is not doing anything...the cron on my server site runs the code fine but the code does not do anything.

 

<?php

$date = ('Y-m-d');

//Auto delete

// Connect to MySQL
$connect = mysql_connect("db","username","password") or die("Not connected");
mysql_select_db("name") or die("could not log in");

// Delete entry  where date equals today from the "example" MySQL table
mysql_query("DELETE FROM boox WHERE date='$date'") 
or die(mysql_error());  


//
?>

Link to comment
Share on other sites

So after it runs, you can view the database, and the records haven't been deleted, right? What is the data type of the `date` field in the table?

Edit your database credentials into this code and post the output, please.

 

$connect = mysql_connect("db","username","password") or die("Not connected");
mysql_select_db("name") or die("could not log in");

$query = "SELECT date FROM boox LIMIT 10";
if( !result = mysql_query($query) ) {
     echo mysql_error();
} else {
     while( $array = mysql_fetch_row($result) ) {
          echo $array[0] . "<br>";
     }
}

Link to comment
Share on other sites

Wait a minute. I just spotted a typo in the original code. $date = ('Y-m-d') is not what you need. It literally assigns the value 'Y-m-d' to $date. Remove that line, then change the query string to this. As long as the date is formatted correctly, this should work just fine.

mysql_query("DELETE FROM boox WHERE date = CURDATE()")

 

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.