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
https://forums.phpfreaks.com/topic/228209-cron-job/
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
https://forums.phpfreaks.com/topic/228209-cron-job/#findComment-1176831
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
https://forums.phpfreaks.com/topic/228209-cron-job/#findComment-1176843
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.