douzhao2047 2017-04-24 20:39
浏览 30

1and1 Cron没有运行mysql查询

I am trying to set up a cron job with my 1and1 hosting. If i write a simple php code, cron runs normal. But when include the database connection and run the code, it does not run.

The code is blow:

    <?php mail('email@gmail.com','At4U test cron','testing cron'); ?>

<?php
    define('__ROOT__', dirname(dirname(__FILE__)));
    require_once(__ROOT__.'/AT4U/extensions/phpmailer/class.phpmailer.php');
    include(__ROOT__.'/AT4U/core/functions/general.php');
    function db_connect() {

        $db_name    = '***************';
        $db_user    = '***************';
        $host       = '***************';
        $password   = '***************';

        // Define connection as a static variable, to avoid connecting more than once 
        static $connection;

        // Try and connect to the database, if a connection has not been established yet
        if(!isset($connection)) {

            $connection = mysqli_connect($host,$db_user,$password,$db_name);
        }

        // If connection was not successful, handle the error
        if($connection === false) {
            // Handle error - notify administrator, log to a file, show an error screen, etc.
            return mysqli_connect_error(); 
        }
        return $connection;
    }

    $con = db_connect();

    $sql = "SELECT * FROM blog WHERE active = 1 ORDER BY id DESC LIMIT 1";
    $result = $con->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $latest_blog = $row['title'];
        }
    }

    email('email@gmail.com','Latest Blog',$latest_blog); 
?>

As you can see, at the top of the page, i inserted a simple email function just to test if the cron works. I get the email every minute. But below this simple code, i wrote the database query and try sending email with the information from database, but it does not work.

Does anyone have any idea why?

  • 写回答

1条回答 默认 最新

  • douqi3064 2017-04-25 08:02
    关注

    (I am assuming the program does work running in the regular environment?)

    A cron job runs in a different environment than web jobs. Some possible causes:

    1. Maybe the datebase stuff IS working, but the email statement at the is not working. The mail(..) statement at the beginning differs from the email(..) statement at the end. Try using the same mail(..) statement at the end.

    2. Try mailing the __ROOT__constant. Is that what you expected?

    3. The working dir is different. I always use the following line at the beginning of my CLI and cron jobs (You might want to remove the .. part depending on your situation.): chdir(realpath(__DIR__ . '/..'));

    4. On my server in case of CLI/cron jobs for instance the following environment variables are empty: DOCUMENT_ROOT, HTTP_HOST, SERVER_SOFTWARE (maybe others too)

    5. Are your included files compatible with running in CLI/cron environment?

    6. It might even use an other php.ini.

    I hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?