在 PDO 的执行函数中使用关联数组将 NULL 值绑定到命名占位符的问题

Problem with binding NULL value to named placeholders with associative array in execute function in PDO

table_nameconditional_clauses 是以 OOP 方式作为参数传递。我通过将其包装在自定义包装器中来使用 PDO。我正在使用带有命名占位符的准备好的语句。在每种情况下,我都会在 PDO->execute() 函数中传递一个关联数组,其中 array_keys 是所用占位符的名称,而 array_value 是要替换的相应值。当我想用 WHERE 子句指定 IS NULL 条件时,我只在一种情况下遇到问题。

基本上,如果我想搜索类似的内容:

SELECT * FROM EMPLOYEES WHERE salary > 10000 AND skill IS NULL

我能够动态构建一个准备好的语句,如下所示:

$sql = SELECT * FROM employees WHERE salary > :salary AND skill IS :skill

然后执行准备好的SQL为:

$stmt->execute(["salary" => 10000,
                "skill" => null])

这就是我面临的问题。仅当值为 null 时,我才在此处收到致命错误。而且,我想在我的包装器中包括检查 IS NULL 功能。

请注意-

以下是供参考的代码片段:

<?php
class DeleteQuery {
        protected function where(array $whereCondition, array &$values): string{
        $whereClause = ' WHERE ';
        $i = 0;
        $j = 0;

        $hasComparators = array_key_exists("comparators", $whereCondition);
        $hasConjunctions = array_key_exists("conjunctions", $whereCondition);

        $comparatorCount = $hasComparators ? count($whereCondition["comparators"]) : 0;
        $conjunctionCount = $hasConjunctions ? count($whereCondition["conjunctions"]) : 0;

        foreach ($whereCondition["predicates"] as $predicate_key => &$predicate_value) {
            $whereClause .= $predicate_key;

            $whereClause .= ($hasComparators and ($i < $comparatorCount)) ?
            ' ' . $whereCondition["comparators"][$i++] . ' ' : ' = ';

            if (is_array($predicate_value)) {
                $whereClause .= "('" . implode("', '", $predicate_value) . "')";
                unset($whereCondition['predicates'][$predicate_key]);
            } else {
                $whereClause .= ':' . $predicate_key;
            }

            $whereClause .= !($hasConjunctions and ($j < $conjunctionCount)) ?
            '' : ' ' . $whereCondition["conjunctions"][$j++] . ' ';
        }

        $values = array_merge($values, $whereCondition['predicates']);

        return $whereClause;
    }

    public function delete($tblName, $conditions) {
        $sql = "DELETE FROM " . $tblName;
        $values = [];

        if (!empty($conditions) && is_array($conditions)) {
            /* If the stmt has WHERE clause */
            if (array_key_exists("where", $conditions)) {
                $sql .= $this->where($conditions['where'], $values);
            }

            /* If the stmt has ORDER BY clause */
            if (array_key_exists("order_by", $conditions)) {
                $sql .= $this->order_by($conditions['order_by']);
            }

            /* If the stmt has LIMIT clause */
            if (array_key_exists("limit", $conditions)) {
                $sql .= $this->limit($conditions['limit'], $values);
            }
        }

        echo $sql . PHP_EOL;
        print_r($values);
    }
}

$deleteConditions = [
    "where" => array(
        "predicates" => ["skill" => null],
        "comparators" => ["IS"],
    ),

    /* other conditional clauses */

];
$obj = new DeleteQuery();
$obj->delete("employees", $deleteConditions);

IS 运算符不能与表达式一起使用。 IS NULLIS NOT NULL 是关键字。

您需要一个适用于 :skill 的空值和非空值的测试。您可以使用 null 安全相等运算符 <=>

$sql = 'SELECT * 
        FROM employees 
        WHERE salary > :salary 
        AND skill <=> :skill';