简体   繁体   中英

PDO Use value null when insert instead of empty

How I can tell PDO to insert NULL instead of an empty value ?

Example :

        $SQL = 'INSERT INTO `Calendars_Events` (`CalendarID`, `AccID`, `DateFrom`, `DateTo`, `Location`, `Title`, `Description`)
                VALUES (:CalendarID, :AccID, :From, :To, :Location, :Title, :Description)';

        $Query = $this->DB->Link->prepare($SQL);
        $Query->bindParam(':CalendarID',        $CalendarID,        PDO::PARAM_INT);
        $Query->bindParam(':AccID',             $this->Account->ID, PDO::PARAM_INT);
        $Query->bindParam(':From',              $From,              PDO::PARAM_INT);
        $Query->bindParam(':To',                $To,                PDO::PARAM_INT);
        $Query->bindParam(':Location',          $Location,          PDO::PARAM_STR);
        $Query->bindParam(':Title',             $Title,             PDO::PARAM_STR);
        $Query->bindParam(':Description',       $Description,       PDO::PARAM_STR);
        $Query->execute();

If Location is empty, it will insert nothing (empty, so '') instead of NULL.

Thanks

如果字符串为空,则传入null而不是空$Location字符串:

empty($Location) ? null : $Location;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM