[英]Bind IS NULL or NULL when using PHP PDO and MySql
How can I bind NULL
in the following scenario. 如何在以下方案中绑定
NULL
。 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. 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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.