【问题标题】:SQLSTATE[HY093]: Invalid parameter number: no parameters were bound, but parameters are providedSQLSTATE[HY093]:参数号无效:未绑定参数,但提供了参数
【发布时间】:2013-12-07 02:14:29
【问题描述】:

我正在构建一个数据库对象,它将PDO 对象与PDOStatement 对象连接起来,以便链接可用。基本上我只放了我最常用的方法,但是bindParam让我很难受。

private $stmt = null;

...

public function prepare($statement,array $driver_options = array()) {
    if($this->stmt) throw new \Exception('PDO Statement already prepared, no override!');
    $this->stmt = parent::prepare($statement, $driver_options);
    return $this;
}

public function bindParam($place, &$val, $dataType){
    if(!$this->stmt) throw new \Exception('PDO Statement is empty');
    $this->stmt->bindParam($place, $val, $dataType);
    return $this;
}

public function execute(array $params = array()){
    if(!$this->stmt) throw new \Exception('PDO Statement is empty');
    $this->stmt->execute($params);
    return $this;
}

public function fetchAll($pdoFetchType){
    if(!$this->stmt) throw new \Exception('PDO Statement is empty');
    return $this->stmt->fetchAll($pdoFetchType);
}

...

public function getStmt(){
    return $this->stmt;
}

public function clearStmt(){
    $this->stmt = null;
}

我在这段代码中从标题中得到错误:

$i = 0;
$db->prepare('SELECT * FROM users LIMIT ?,1')->bindParam(1, $i, \PDO::PARAM_INT);
while($row = $db->execute()->fetchAll(\PDO::FETCH_ASSOC)){
    echo "<pre>".print_r($row, true)."</pre>";
    $i++;
}

基本上我发现这个错误是当bindParam 中提供的变量是null,但$i 显然不为空时发生。你能帮帮我吗?

编辑:也在运行

var_dump($this->stmt->bindParam($place, $val, $dataType));

bindParam 方法中返回TRUE。来自手册:

返回值

成功时返回 TRUE,失败时返回 FALSE。

成功了但是没有绑定参数???我觉得我的大脑快要爆炸了。

【问题讨论】:

  • 如果你把var_dump($val) 放在你定义的bindParam() 里面会发生什么?如果你把var_dump($this-&gt;stmt)?
  • @FranciscoPresencia int(0) object(PDOStatement)#6 (1) { ["queryString"]=&gt; string(29) "SELECT * FROM users LIMIT ?,1" } 我真的很讨厌php 对我这样做..
  • 我的想法已经不多了,请尝试在您的函数 public function bindParam 中使用 bindValue() 而不是 bindParam() 以查看是否有变化。从文档中,与 PDOStatement::bindValue() 不同,该变量被绑定为引用,并且只会在调用 PDOStatement::execute() 时进行评估。 但我不确定如果它会改变任何东西。
  • @FranciscoPresencia 即使错误消息保持不变,至少可以改变.. Q_Q
  • 调试器是你的朋友。

标签: php pdo


【解决方案1】:

我猜想使用引用 &amp;$val 而不是值 $val 是导致问题的原因。

请尝试以下代码:

public function bindParam($place, $val, $dataType)
{
    if(!$this->stmt) throw new \Exception('PDO Statement is empty');
    $this->stmt->bindParam($place, $val, $dataType);
    return $this;
}

编辑

我上面的答案是错误的。

尝试修改execute方法:

public function execute(array $params = array()){
    if(!$this->stmt) throw new \Exception('PDO Statement is empty');
    $this->stmt->execute();
    return $this;
}

将一个空数组作为参数传递给execute 方法会删除所有以前的绑定。这就是为什么bindParam 返回true(成功绑定),但是一调用execute 就会出现“没有绑定参数”错误。

【讨论】:

  • 以前没有引用,还是报同样的错误。我添加了引用,因为没有它,像这样包装父 bindParam 是没有意义的
  • 引用很危险,很容易破坏。请尝试运行您的循环并在每次迭代中使用更新的$i 值调用bindParam,而不是依赖参考。我确信它会按预期工作。
  • 它没有。正确使用引用并不危险。如果原始的PDO 函数bindParam 使用引用,那么必须有一个很好的理由,因为有。无论如何我尝试了你的建议,遗憾的是,错误是一样的。
  • 我在此撤回关于您所谓的参考滥用的声明 - 请参阅我编辑的答案。
  • 其中一件事情让你想去“我怎么没看到这个”。非常感谢楼主!
【解决方案2】:

遇到同样的错误,但原因不同

我的请求有 cmets,其中一个包含一个该死的问号
对于 PDO,“?”当然是要绑定的参数。

我的请求在其他任何地方都没有问题,而且我不知道 PDO 会在哪里发明一个“参数”,而我没有使用任何参数,因为我总是使用命名占位符,例如 :value


在这上面花了一个多小时:(
希望这个答案能帮助一些遇到这个愚蠢琐碎问题的人。

【讨论】:

  • 哦,很好 - 谢谢! Yii 1.1 在将服务器升级到 PHP7.2 和 MySQL 5.7 时遇到了同样的问题。
猜你喜欢
  • 1970-01-01
  • 2012-06-13
  • 2012-04-15
  • 2020-03-18
  • 2016-11-28
相关资源
最近更新 更多