我如何告诉PDO插入NULL而不是空值?
示例:
$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.
谢谢
解决方法:
如果字符串为空,则传入null而不是空$Location字符串:
empty($Location) ? null : $Location;
标签:php,mysql,pdo
来源: https://codeday.me/bug/20190723/1513166.html