【问题标题】:PDO query builder class execute() error SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokensPDO 查询构建器类 execute() 错误 SQLSTATE[HY093]: Invalid parameter number: 绑定变量数与令牌数不匹配
【发布时间】:2015-09-15 03:52:45
【问题描述】:

我正在尝试创建一个 PDO 查询构建器类,但它在更新功能中不起作用。我得到错误:

警告:PDOStatement::execute(): SQLSTATE[HY093]: 参数号无效:绑定变量的数量与标记的数量不匹配

谁能告诉我如何使它工作。谢谢

class query extends PDO {

private $_statement;
private $_prepare;

public function __construct() {
    parent::__construct(DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
    $this->_statement = "";
    $this->_prepare = NULL;
}

function UPDATE($table) {
    $this->_prepare = $this->prepare("UPDATE " . $table);
    $this->_statement = $this->_prepare->queryString;
    return $this;
}

function DATA($data) {
    ksort($data);
    if (strstr($this->_statement, "UPDATE")) {
        $fieldDetails = null;
        foreach ($data as $key => $value) {
            $fieldDetails.="$key = :$key, ";
        }
        $fieldDetails = rtrim($fieldDetails, ', ');
        $this->_prepare = $this->prepare($this->_statement . " SET " . $fieldDetails);
        foreach ($data as $key => $value) {
            $this->_prepare->bindValue(":$key", $value);
        }
    }
    $this->_statement = $this->_prepare->queryString;
    return $this;
}

function WHERE($where, $operator = "AND") {
    $condition = null;
    if (is_array($where)) {
        if (count($where) != 0) {
            foreach ($where as $key => $value) {
                $condition.="$key = :$tempK " . $operator . " ";
            }
        }
    }
    $condition = rtrim($condition, $operator . " ");

    $this->_prepare = $this->prepare($this->_statement . " WHERE " . $condition);
    foreach ($where as $key => $value) {
        $this->_prepare->bindValue(":$tempK", $value);
    }
    $this->_statement = $this->_prepare->queryString;
    return $this;
}

function send() {
    return  $this->_prepare->execute();
    }
}

这是测试功能

$q = new query();
$q->UPDATE("user")
->DATA(array("email" => "dd"))
->WHERE(array("username"=>"ee"))
->send();

【问题讨论】:

  • 好吧,当你正在构建它时,你可能知道错误意味着什么问题。 mn

标签: php mysql pdo


【解决方案1】:

谢谢大家,我自己找到了解决方案,想分享一下我的解决方案。

这是代码更新

    class query extends PDO {

private $_statement;
private $_prepare;
private $_values;

public function __construct() {
    parent::__construct(DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
    $this->_statement = "";
    $this->_prepare = NULL;
    $this->_values = array();
}

function UPDATE($table) {
    $this->_prepare = $this->prepare("UPDATE " . $table);
    $this->_statement = $this->_prepare->queryString;
    return $this;
}

function DATA($data) {
    ksort($data);
    if (strstr($this->_statement, "UPDATE")) {
        $fieldDetails = null;
        foreach ($data as $key => $value) {
            $fieldDetails.="$key = :$key, ";
        }
        $fieldDetails = rtrim($fieldDetails, ', ');
        $this->_prepare = $this->prepare($this->_statement . " SET " . $fieldDetails);
        foreach ($data as $key => $value) {
            $this->_values[":$key"] = $value;
        }
    }
    $this->_statement = $this->_prepare->queryString;
    return $this;
}

function WHERE($where, $operator = "AND") {
    $condition = null;
    if (is_array($where)) {
        if (count($where) != 0) {
            foreach ($where as $key => $value) {
                $condition.="$key = :where$key " . $operator . " ";
            }
        }
    }
    $condition = rtrim($condition, $operator . " ");

    $this->_prepare = $this->prepare($this->_statement . " WHERE " . $condition);
    foreach ($where as $key => $value) {
        $this->_values[":where$key"] = $value;
    }
    $this->_statement = $this->_prepare->queryString;
    return $this;
}

function send() {
    return  $this->_prepare->execute($this->_values);
    }
}

我认为问题可能是 prepare 语句不能在单独的函数中使用 bindValue()。此外,execute() 的参数允许我将所有绑定值放入准备语句中。 终于,问题解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多