php SQLSTATE[HY093]:参数号无效:没有绑定参数,但提供了参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20436745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLSTATE[HY093]: Invalid parameter number: no parameters were bound, but parameters are provided
提问by php_nub_qq
I'm building a database object that joins the PDOobject with the PDOStatementobject in order for chaining to be available. Basically I just put the methods I most frequently use, but bindParamis giving me a hard time.
我正在构建一个数据库对象,该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 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++;
}
Basically what I found out about this error is that it occurs when provided variables in bindParamare null, but $iis clearly not null. Can you help me out?
基本上我发现这个错误是在bindParamare 中提供变量时发生null,但$i显然不是空的。你能帮我吗?
EDIT: Also running
编辑:也在运行
var_dump($this->stmt->bindParam($place, $val, $dataType));
in the bindParammethod returns TRUE. From the manual:
在bindParam方法中返回TRUE。从手册:
Return Values
Returns TRUE on success or FALSE on failure.
返回值
成功时返回 TRUE,失败时返回 FALSE。
It's succeeding but not binding the parameter ??? I feel my brain is going to explode soon.
它成功了但没有绑定参数???我觉得我的大脑很快就要爆炸了。
采纳答案by mingos
I guess using a reference &$valinstead of a value $valis what causes the issue.
我想使用引用&$val而不是值$val是导致问题的原因。
Please try this code instead:
请改用此代码:
public function bindParam($place, $val, $dataType)
{
if(!$this->stmt) throw new \Exception('PDO Statement is empty');
$this->stmt->bindParam($place, $val, $dataType);
return $this;
}
EDIT
编辑
My above answer is wrong.
我上面的回答是错误的。
Try modifying the executemethod:
尝试修改execute方法:
public function execute(array $params = array()){
if(!$this->stmt) throw new \Exception('PDO Statement is empty');
$this->stmt->execute();
return $this;
}
Passing an empty array as parametre to the executemethod removes all previous bindings. This is why bindParamreturned true (successfully bound), yet the "no params were bound" error appeared as soon as you called execute.
将空数组作为参数传递给该execute方法会删除所有先前的绑定。这就是为什么bindParam返回 true(成功绑定),但在您调用execute.
回答by Balmipour
Got the same error, but different cause
有同样的错误,但不同的原因
My request had comments, one of which included a damn question mark.
For PDO, a "?" is, of course, a parameter to bind.
我的请求有评论,其中一个包括一个该死的问号。
对于 PDO,“ ?”当然是要绑定的参数。
My request had no issue anywhere else, and I had no idea where PDO would invent a "parameter" while I wasn't using any, since I always use named placeholders, like:value
我的请求在其他任何地方都没有问题,我不知道 PDO 会在哪里发明一个“参数”,而我没有使用任何参数,因为我总是使用命名占位符,比如:value
Spent more than one hour on this :(
May this answer help some people having this foolishly trivial issue.
在这个上花了一个多小时:(
这个答案可以帮助一些有这个愚蠢琐碎问题的人。

