Home > Mobile >  PHP Writing to database gives bool(false) using Cron, but not in local
PHP Writing to database gives bool(false) using Cron, but not in local

Time:08-19

I'm trying to run a function to save user orders. In local the variable $place_orders gives bool(true) and everything works fine. Now, I'm trying to run the same function using a cron job. The cron job file can locate all the necessary files. Unfortunately here the $place_orders gives bool(false) and no other errors.

I have tried displaying all errors, but it only displayed undefined indexes SERVER_NAME, SERVER_PORT and QUERY_STRING which I then did define.

What goes wrong here?

$db = Database::newInstance();

$data = array();
$data['order_id']   = $order_id;
$data['order_date'] = date("Y-m-d H:i:s");
$data['product_id'] = $order->product_id;
$data['user_id']    = $user_id;

$query = "insert into order_details (order_id,order_date,product_id,user_id) values (:order_id,:order_date,:product_id,:user_id)";
$place_orders = $db->write($query,$data);

This is my database class:

Class Database
{
    public static $con;

    public function __construct()
    {
        try{
 
            $string = DB_TYPE . ":host=". DB_HOST .";dbname=". DB_NAME;
            self::$con = new PDO($string,DB_USER,DB_PASS);

        }catch (PDOException $e){

            die($e->getMessage());
        }
    }

    public static function getInstance()
    {
        if(self::$con){

            //return self::$con;
        }

        return $instance = new self();
    }

    public static function newInstance()
    {
        return $instance = new self();
    }


    /*
    * read from database
    */
    public function read($query,$data = array())
    {

        $stm = self::$con->prepare($query);
        $result = $stm->execute($data);

        if($result){
            $data = $stm->fetchAll(PDO::FETCH_OBJ);
            if(is_array($data) && count($data) > 0)
            {
                return $data;
            }
        }

        return false;
    }

    /*
    * write to database
    */
    public function write($query,$data = array())
    {

        $stm = self::$con->prepare($query);
        $result = $stm->execute($data);
        
        if($result){
             
            return true;
        }

        return false;
    }
}

This is my cron script file:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once("/home/customer/www/mydomain.com/app/init.php");

Class Cron extends controller
{  
    public function run_cron(){
        
        $order = $this->load_model('Order');

        $date = date('2022-08-08');
        $today = date('j', strtotime($date));
        //$today = date('j');

        if($today == 8 || $today == 25){
            $order->save_monthly_order();
        }
    }
}

$cron = new cron();
$cron->run_cron();

CodePudding user response:

Did you check the PDO error info for the statement?

https://www.php.net/manual/de/pdostatement.errorinfo.php

Something like this should tell you if there is a pdo error and what type.

<?php
       $stm = self::$con->prepare($query);
        $result = $stm->execute($data);

echo "\nPDOStatement::errorInfo():\n";
$arr = $stm->errorInfo();
print_r($arr);
?>

Are you using any environment variables which might not be defined for the cron user/environment.

If the QUERY_STRING variable is acccessed it seems like frontend code is excuted which probably shouldn't on a cron run.

Did you define those server variables manually to fix this? If so it might be better if you look which class is throwing the error and implement a early return there, so that the frontend code is not run.

if (!isset($_SERVER)) {
 return;
}
  • Related