SQLSTATE [HY093]:无效的参数号:未绑定任何参数,但提供了参数 [英] SQLSTATE[HY093]: Invalid parameter number: no parameters were bound, but parameters are provided

查看:75
本文介绍了SQLSTATE [HY093]:无效的参数号:未绑定任何参数,但提供了参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个数据库对象,该对象将PDO对象与PDOStatement对象连接在一起,以便可以使用链接.基本上,我只是使用最常用的方法,但是bindParam给我带来了困难.

I'm building a database object that joins the PDO object with the PDOStatement object in order for chaining to be available. Basically I just put the methods I most frequently use, but bindParam is giving me a hard time.

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 get the error, from the title, in this code:

$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显然不为null.你能帮我吗?

Basically what I found out about this error is that it occurs when provided variables in bindParam are null, but $i is clearly not null. Can you help me out?

编辑:还在运行

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

bindParam方法中的

返回TRUE.从手册中:

in the bindParam method returns TRUE. From the manual:

返回值

成功返回TRUE,失败返回FALSE.

Returns TRUE on success or FALSE on failure.

成功,但未绑定参数???我觉得我的大脑快要爆炸了.

It's succeeding but not binding the parameter ??? I feel my brain is going to explode soon.

推荐答案

我猜想是使用引用&$val代替值$val引起问题的原因.

I guess using a reference &$val instead of a value $val is what causes the issue.

请改用以下代码:

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

编辑

我的以上答案是错误的.

My above answer is wrong.

尝试修改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时立即出现没有绑定参数"错误的原因.

Passing an empty array as parametre to the execute method removes all previous bindings. This is why bindParam returned true (successfully bound), yet the "no params were bound" error appeared as soon as you called execute.

这篇关于SQLSTATE [HY093]:无效的参数号:未绑定任何参数,但提供了参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆