[Solved] Proper use of NULL in mysql query from php


NULL is a specific value, not a string literal. It should be passed to query directly, i.e.

INSERT INTO `Schedule` (`ETA`,`STA`,`ATA`)
    VALUES ('2013-08-28 12:30:00', NULL, NULL);

that means your PHP code should handle that and not enclose NULL-s:

$timeFormatAndNull = function ($format) {
    return function($time) use ($format) {
        $time = strtotime($time);
        return $time ? strftime($format, $time) : 'NULL';
    };
};


$operationalLocalDate = function($arrivals, $callback) {
    return function($i, $date) use ($arrivals, $callback) {
        return $callback(
           $arrivals[$i]["operationalTimes"][$date]["dateLocal"])
        );
    };
};

and

$formatTime = $operationalLocalDate(
    $arrivals
    $timeFormatAndNull("'%Y-%m-%d %H:%M:%S'")
);

$query = sprintf(
    "INSERT INTO `Schedule` (`ETA`,`STA`,`ATA`) VALUES (%s, %s, %s);"
    , $formatTime($i, "estimatedGateArrival")
    , $formatTime($i, "scheduledGateArrival")
    , $formatTime($i, "actualGateArrival")
);

2

solved Proper use of NULL in mysql query from php