How can I bind NULL
in the following scenario. No matter what I do, I can't get it to work.
if ($type == 1) {
$self = 'NULL';
$parent = 'NULL';
}
$findThis = $conn->prepare('select count(id) as exists from table where id = :id and self = :self and parent = :parent';
$findThis->execute(array(
":id" => $id,
":self" => $self,
":parent" => $parent
));
UPDATE. By an by I have learned that all can be done in one query
$sql = 'select 1 from table where id = ? and self <=> ? and parent <=> ?';
$stm = $conn->prepare($sql);
$stm->execute([$id,$self,$parent]);
$exists = $stm->fetchColumn();
You have to amend your query as well.
$sql = 'select count(id) as exists from table where id = ? and '; if ($type == 1) { $sql .= 'self IS NULL and parent IS NULL' $data = [$id]; } else { $sql .= 'self = ? and parent = ?'; $data = [$id,$self,$parent]; } $stm = $conn->prepare($sql); $stm->execute($data);
对于空值,您必须在sql中使用IS NULL
。
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.