PDO - Invalid parameter number(PDO - 无效的参数号)
问题描述
就在最近,我在 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 - 无效的参数号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PDO - 无效的参数号
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01