繁体   English   中英

使用PHP PDO和MySql时绑定IS NULL或NULL

[英]Bind IS NULL or NULL when using PHP PDO and MySql

如何在以下方案中绑定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
));

UPDATE。 通过我已经了解到所有可以在一个查询中完成

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM