php - PDOStatement::bindValue- SQLSTATE[HY093]:参数编号无效

标签 php mysql pdo

在对 SQLSTATE[HY093]: Invalid parameter number 问题进行故障排除后,来自下面的准备语句。
我发现 $stmt->bindValue 有问题。


准备好的语句和 BindValue - 1

$stmt = $conn->prepare(
   'SELECT *
    FROM `gcm_notification` t1
    RIGHT JOIN (
        SELECT MAX(`id`) AS `latest` 
        FROM `gcm_notification` 
        WHERE `registration_id` = :registration_id 
        GROUP BY `post_id`) t2
    ON `t1`.`id` = `t2`.`latest`
    LEFT JOIN `posts` t3 ON `t1`.post_id = `t3`.ID
    WHERE `registration_id` = :registration_id
    GROUP BY `post_id`'
);

$stmt->bindValue(':registration_id', $registration_id, PDO::PARAM_STR);
$stmt->execute();

结果
SQLSTATE[HY093]:参数编号无效


准备好的语句和 BindValue - 2

$stmt = $conn->prepare(
   'SELECT *
    FROM `gcm_notification` t1
    RIGHT JOIN (
        SELECT MAX(`id`) AS `latest` 
        FROM `gcm_notification` 
        WHERE `registration_id` = :registration_id 
        GROUP BY `post_id`) t2
    ON `t1`.`id` = `t2`.`latest`
    LEFT JOIN `posts` t3 ON `t1`.post_id = `t3`.ID
    WHERE `registration_id` = :registration_id
    GROUP BY `post_id`'
);

$stmt->bindValue(':registration_id', $registration_id, PDO::PARAM_STR);
$stmt->bindValue(':registration_id', $registration_id, PDO::PARAM_STR);
$stmt->execute();

结果
SQLSTATE[HY000]:一般错误:2031


准备好的语句和 BindValue - 3

$stmt = $conn->prepare(
   'SELECT *
    FROM `gcm_notification` t1
    RIGHT JOIN (
        SELECT MAX(`id`) AS `latest` 
        FROM `gcm_notification` 
        WHERE `registration_id` = :registration_id 
        GROUP BY `post_id`) t2
    ON `t1`.`id` = `t2`.`latest`
    LEFT JOIN `posts` t3 ON `t1`.post_id = `t3`.ID
    WHERE `registration_id` = :registration_id_1
    GROUP BY `post_id`'
);

$stmt->bindValue(':registration_id', $registration_id, PDO::PARAM_STR);
$stmt->bindValue(':registration_id_1', $registration_id, PDO::PARAM_STR);
$stmt->execute();

结果
成功


我不明白为什么 Prepared Statement & BindValue - 1 不起作用。 bindValue 不能绑定(bind) 2 个相同的参数 :registration_id?

因此,我假设我对第一点的假设是正确的,我复制并粘贴了另一个 bindValue 并尝试使用 Prepared Statement & BindValue - 2。结果又失败了。

我猜可能是因为参数有冲突。因此,我将参数重命名为 registration_idregistration_id_1,即 Prepared Statement & BindValue - 3 并且它正在运行。

有人介意向我解释一下到底发生了什么吗?

谢谢你,非常感谢。

最佳答案

感谢@Jon Stirling

阅读关于 PHP Manual 的评论后.

引自william dot clarke at gmail dot com

Surely if you want to use prepared statements that way you should use the syntax in the second example:

eg.

instead of: select id,name from demo_de where name LIKE :name OR name=:name

use: select id,name from demo_de where name LIKE ? OR name=?

I believe you are supposed to either use distinct named parameters (name, name1) OR anonymous parameters (?s)

他大约在 9 年前回答了我的问题。

希望这篇文章对正在寻找答案的人有所帮助。

关于php - PDOStatement::bindValue- SQLSTATE[HY093]:参数编号无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36909605/

相关文章:

javascript - 使用 ajax 的 jQuery 调用没有响应,没有错误

mysql - SELECT 命令拒绝用户 'user' @'localhost' 表 'table'

mysql - 无法使用 Doctrine 和 Symfony2 在 MySQL 中创建表

php - 跟踪一个月前的结果并获取每天的结果 - MySQL

php - 如何使用PDO传递参数调用存储过程函数

mysql - PDO 准备语句 "Invalid Parameter Number"

php - 移动应用程序如何使用 API 并证明其要修改的 Facebook ID 已在 Facebook 上登录并获得授权?

php - Mysql 坚持将奇怪的不间断空格字符放入我的空文本区域中

php - 如何将数组(不同)插入到 mySQL 中?

mysql - 如何在一个数据库表中查找某个字段的值与另一表中的值不同的行