使用PHP PDO和MySql时绑定IS NULL或NULL [英] Bind IS NULL or NULL when using PHP PDO and MySql

查看:88
本文介绍了使用PHP PDO和MySql时绑定IS NULL或NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下情况下如何绑定NULL.无论我做什么,我都无法正常工作.

How can I bind NULL in the following scenario. 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. 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);

这篇关于使用PHP PDO和MySql时绑定IS NULL或NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆