簡體   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