就在最近,我在 PHP/MySQL 中改用 PDO 并转换了几十个查询.他们中的大多数都有效,但是这个非常简单的方法会在 $sql->execute()
$sql=$pdo->prepare("SELECT id FROM user WHERE username = :username LIMIT 1");$sql->execute(array(':username',$username));
<块引用>
PDOStatement::execute() pdostatement.execute SQLSTATE[HY093]:无效的参数号:绑定变量的数量与......中的标记数量不匹配
经过研究,我找到了这个链接:https://bugs.php.net/bug.php?id=60515
... 因此尝试将查询更改为
$sql=$pdo->prepare("SELECT `id` FROM `user` WHERE `username` = :username LIMIT 1");$sql->execute(array(':username',$username));
但结果还是一样.有没有人看到明显错误的地方,或者为什么这个查询在所有其他人都这样做时不起作用?
在此先非常感谢您!
':username',$username
仅适用于 bindParam() 方法:
$sql->bindParam(':username', $username, PDO::PARAM_STR);
看看这里:http://www.php.net/手册/en/pdostatement.bindparam.php
对于执行,您需要传递正确的仅输入值数组:
$sql->execute(array(':username' => $username));
占位符:
你也可以使用这个:
$sql->execute(array($username));
但为此,您需要将查询更改为:
$sql=$pdo->prepare("SELECT `id` FROM `user` WHERE `username` = ?LIMIT 1");
?用作占位符并从数组中获取变量.当您在 SQL 语句中使用更多占位符时,该函数会按顺序从数组中取出所有变量.
Just recently I've switched to using PDO in PHP/MySQL and transformed some dozens of queries. Most of them worked, however this very easy one throws an exception at $sql->execute()
$sql=$pdo->prepare("SELECT id FROM user WHERE username = :username LIMIT 1");
$sql->execute(array(':username',$username));
PDOStatement::execute() pdostatement.execute SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in ...
After research, I found this link: https://bugs.php.net/bug.php?id=60515
... and therefore tried to change the query to
$sql=$pdo->prepare("SELECT `id` FROM `user` WHERE `username` = :username LIMIT 1");
$sql->execute(array(':username',$username));
But still with the same result. Does anybody see what is obviously wrong or why does this query not work when all others did?
Thank you very much in advance!
The ':username',$username
works only in bindParam() method:
$sql->bindParam(':username', $username, PDO::PARAM_STR);
Take a look here: http://www.php.net/manual/en/pdostatement.bindparam.php
For execute you need to pass a correct array of input-only values:
$sql->execute(array(':username' => $username));
Placeholder:
You can also use this:
$sql->execute(array($username));
But for this you need to change your query to this:
$sql=$pdo->prepare("SELECT `id` FROM `user` WHERE `username` = ? LIMIT 1");
The ? works as palceholder and take the variables from the array. When you use more placeholder in your SQL statement the function takes all the variables out of the array in it's order.
这篇关于PDO - 无效的参数号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!