Sweepster
Sweepster

Reputation: 1949

CRON isn't working

I have the following php code set to run as a CRON job. It runs once a day and never returns an error so I assume it is running properly. The problem is, the rows aren't being deleted from my database!

I've tested the php and the variables work. I've tested my query and that works too so I'm at a loss...

<?php
$isCLI = ( php_sapi_name() == 'cgi-fcgi' );

if (!$isCLI)
    die("cannot run! sapi_name is ".php_sapi_name());
exit;

//Connect to DB
mysql_connect("xxxxxx.com", "xxxxxxxxxx", "xxxxxxxxxxxx") or die(mysql_error());
mysql_select_db("xxxxxxxxxxx") or die(mysql_error());

//Get today's date
$todayis = date("Y-m-d");
$todayis = strtotime(date("Y-m-d", strtotime($todayis)) . " -4 hours");
$todayis = date('Y-m-d', $todayis);

//Delete rows in userContests where reminder is less than or equal to today's date
mysql_query("DELETE FROM userContests WHERE reminder <= '$todayis';") or die(mysql_error());
?>

Can someone explain to me why the rows won't delete?

Upvotes: 0

Views: 270

Answers (2)

jeroen
jeroen

Reputation: 91744

If that is the whole script, I would say you have forgotten to connect to the database.

Edit: This seems to be the problem:

if (!$isCLI)
    die("cannot run! sapi_name is ".php_sapi_name());
exit;

That translates to:

if (!$isCLI) 
{
    die("cannot run! sapi_name is ".php_sapi_name());
}

exit;

So basically you always stop your script on the 6th line, nothing after that will ever be executed.

Upvotes: 1

Xeoncross
Xeoncross

Reputation: 57204

If it is a CRON job, it should be using PHP-CLI not PHP-CGI. You can add this to the top of the file to do the right check.

<?php if(PHP_SAPI != 'cli') throw new Exception('Not in CLI mode!');

...

Upvotes: 0

Related Questions