0

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

当我运行查询时,它给了我一个错误:警告:PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\Media\admin第 35 行的 \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';
        }
    }
}
4

1 回答 1

0

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

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

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

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

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

于 2016-03-01T09:49:59.547 回答