如何在以下情况下绑定(bind) NULL
。无论我做什么,我都无法让它发挥作用。
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
));
最佳答案
更新。通过一个 by 我了解到所有的事情都可以在一个查询中完成
$sql = 'select 1 from table where id = ? and self <=> ? and parent <=> ?';
$stm = $conn->prepare($sql);
$stm->execute([$id,$self,$parent]);
$exists = $stm->fetchColumn();
您还必须修改您的查询。
$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);
关于php - 使用 PHP PDO 和 MySql 时绑定(bind) IS NULL 或 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18735955/