1

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
));
4

2 回答 2

3

更新。通过我了解到,所有事情都可以在一个查询中完成

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

于 2013-09-11T08:48:53.727 回答
1

对于空值,您必须改为IS NULL在 sql 中使用。

于 2013-09-11T08:18:47.457 回答