PHP binding values


PHP binding values

我在绑定多个值时遇到问题。我已经尝试了一个值,它工作正常。我错过了什么吗?

当我运行查询时,它给了我一个错误:警告:PDOStatement::execute(): SQLSTATE[HY093]:参数编号无效:绑定变量的数量与第 35 行的 C:''xampp''htdocs''Media''admin''dashboard''classes''DB.php 中的标记数量不匹配

private function runQuery($sql, $bind_value = array()) {
    $this->_error = false;
    if($ms = $this->_query = $this->_pdo->prepare($sql)) {
        $this->_query->bindValue($x, $bind_values);
        if($this->_query->execute()) { //**ERROR FOUND HERE**
            echo 'ok';
        }
        die();
    }
}
public function get($column, $table, $where = array()) {
    if($where) {
        if(count($where) === 3) {
            $operators = array(
                '=',
                '<',
                '>',
                '>=',
                '<='
            );
            $field      = $where[0];
            $operator   = $where[1];
            $value      = $where[2];
            $bind_value = array(
                $column,
                $table,
                $field,
                $operator,
                $value,
            );
            if(in_array($operator, $operators)) {
                $sql = "SELECT ? ? WHERE ? ? ?";
                return $this->runQuery($sql, $bind_value);
            }
        } else {
            echo 'problem';
        }
    }
}

你在这里几乎违反了OOP的每一条规则。 你的类中不应该有像_query这样的变量。

你真正需要的只是这五行:

public function runQuery($sql, $bind_value = array()) {
    $stmt = $this->_pdo->prepare($sql);
    $stmt->execute($bind_value);
    return $stmt;
}

它必须返回一个语句,而不是将其设置为类变量。

get() 函数也是无用和错误的,你最好避免它。如果你真的想要一个函数来自动选择查询,你必须查看现有的查询构建器,如AuraSQL。