duanli9591 2014-12-13 17:40
浏览 81
已采纳

PHP的PDO准备声明:我能多次使用一个占位符吗? [重复]

This question already has an answer here:

I'd like to perform the following query:

    SELECT
      *,
      (SELECT COUNT(*) FROM `tab2` WHERE `parent` = :id) AS `sum`
    FROM `tab1`
    WHERE `id` = :id

As you can see :id placeholder appeared twice in the query. So if I'd try to execute this statement with:

$q->execute(['id'=>$row_id]);

I'm receiving the error:

Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number

So I have to rewrite the prepared query and execute array with :id1 and :id2 placeholders which looks a bit stupid for me.

Is it the only way to use one placeholder in several places of the prepared statement?

</div>
  • 写回答

1条回答 默认 最新

  • dongqianchi0512 2014-12-14 16:48
    关注

    PDO::prepare states that

    [y]ou cannot use a named parameter marker of the same name more than once in a prepared statement, unless emulation mode is on.

    Since it's generally better to leave emulation mode off (so the database does the prepared statement), you'll have to use id_0, id_1, etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊