3

空の値の代わりにNULLを挿入するようにPDOに指示するにはどうすればよいですか?

例 :

        $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();

空の場合Location、NULLの代わりに何も挿入されません(空なので'')。

ありがとう

4

1 に答える 1

9

文字列が空の場合はnull、空の文字列の代わりに渡します。$Location

empty($Location) ? null : $Location;
于 2012-04-20T18:55:44.833 に答える