Proper use of NULL in mysql query from php -


$eta_time =  strtotime($arrivals[$i]["operationaltimes"]["estimatedgatearrival"]["datelocal"]); if (!$eta_time) {     $eta = 'null'; } else {     $eta = strftime("%y-%m-%d %h:%m:%s", $eta_time); }  $sta_time   = strtotime($arrivals[$i]["operationaltimes"]["scheduledgatearrival"]["datelocal"]); if (!$sta_time) {     $sta = 'null'; } else {     $sta = strftime("%y-%m-%d %h:%m:%s", $sta_time); }  $ata_time = strtotime($arrivals[$i]["operationaltimes"]["actualgatearrival"]["datelocal"]); if (!$ata_time) {     $ata = 'null'; } else {     $ata = strftime("%y-%m-%d %h:%m:%s", $ata_time); }  $query="insert `schedule` (`eta`,`sta`,`ata`)             values('".$eta."','".$sta."','".$ata."');"; $result=run_query($query); 

the related mysql db table:

create table `schedule` (   `id` smallint(6) not null auto_increment,   `sta` datetime default null,   `eta` datetime default null,   `ata` datetime default null, ); 

query var_dump:

insert `schedule` (`eta`,`sta`,`ata`) values('2013-08-28 12:30:00','null','null'); 

the error message is:

incorrect datetime value: 'null' column 'sta' @ row 1 

null specific value, not string literal. should passed query directly, i.e.

insert `schedule` (`eta`,`sta`,`ata`)     values ('2013-08-28 12:30:00', null, null); 

that means php code should handle , 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 `schedule` (`eta`,`sta`,`ata`) values (%s, %s, %s);"     , $formattime($i, "estimatedgatearrival")     , $formattime($i, "scheduledgatearrival")     , $formattime($i, "actualgatearrival") ); 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -