0

次のphpコードをCRONジョブとして実行するように設定しています。1日に1回実行され、エラーが返されることはないため、正常に実行されていると思います。問題は、行がデータベースから削除されていないことです。

私はphpをテストし、変数が機能します。私は自分のクエリをテストしましたが、それも機能するので、途方に暮れています...

<?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());
?>

行が削除されない理由を誰かが私に説明できますか?

4

2 に答える 2

1

それがスクリプト全体である場合、データベースへの接続を忘れていると思います。

編集:これは問題のようです:

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

これは次のように解釈されます。

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

exit;

したがって、基本的には常にスクリプトを6行目で停止し、それ以降は何も実行されません。

于 2012-05-08T00:56:25.437 に答える
0

CRONジョブの場合は、notを使用する必要がPHP-CLIありPHP-CGIます。これをファイルの先頭に追加して、正しいチェックを行うことができます。

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

...
于 2012-05-08T00:56:53.623 に答える