<legend id='RpZLT'><style id='RpZLT'><dir id='RpZLT'><q id='RpZLT'></q></dir></style></legend>

  • <tfoot id='RpZLT'></tfoot>
    • <bdo id='RpZLT'></bdo><ul id='RpZLT'></ul>
  • <small id='RpZLT'></small><noframes id='RpZLT'>

      1. <i id='RpZLT'><tr id='RpZLT'><dt id='RpZLT'><q id='RpZLT'><span id='RpZLT'><b id='RpZLT'><form id='RpZLT'><ins id='RpZLT'></ins><ul id='RpZLT'></ul><sub id='RpZLT'></sub></form><legend id='RpZLT'></legend><bdo id='RpZLT'><pre id='RpZLT'><center id='RpZLT'></center></pre></bdo></b><th id='RpZLT'></th></span></q></dt></tr></i><div id='RpZLT'><tfoot id='RpZLT'></tfoot><dl id='RpZLT'><fieldset id='RpZLT'></fieldset></dl></div>

        PHP 的 PDO 准备语句:我可以多次使用一个占位符吗

        时间:2023-09-20
      2. <legend id='zo8GQ'><style id='zo8GQ'><dir id='zo8GQ'><q id='zo8GQ'></q></dir></style></legend>
          <i id='zo8GQ'><tr id='zo8GQ'><dt id='zo8GQ'><q id='zo8GQ'><span id='zo8GQ'><b id='zo8GQ'><form id='zo8GQ'><ins id='zo8GQ'></ins><ul id='zo8GQ'></ul><sub id='zo8GQ'></sub></form><legend id='zo8GQ'></legend><bdo id='zo8GQ'><pre id='zo8GQ'><center id='zo8GQ'></center></pre></bdo></b><th id='zo8GQ'></th></span></q></dt></tr></i><div id='zo8GQ'><tfoot id='zo8GQ'></tfoot><dl id='zo8GQ'><fieldset id='zo8GQ'></fieldset></dl></div>

          <small id='zo8GQ'></small><noframes id='zo8GQ'>

            <bdo id='zo8GQ'></bdo><ul id='zo8GQ'></ul>
              <tbody id='zo8GQ'></tbody>
            <tfoot id='zo8GQ'></tfoot>

                • 本文介绍了PHP 的 PDO 准备语句:我可以多次使用一个占位符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想执行以下查询:

                  选择*,(SELECT COUNT(*) FROM `tab2` WHERE `parent` = :id) AS `sum`从`tab1`哪里`id` = :id

                  如您所见,:id 占位符在查询中出现了两次.因此,如果我尝试使用以下命令执行此语句:

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

                  我收到错误:

                  致命错误:未捕获的异常PDOException",消息为SQLSTATE[HY093]:参数编号无效

                  所以我必须重写准备好的查询并使用 :id1 和 :id2 占位符执行数组,这对我来说看起来有点愚蠢.

                  这是在准备好的语句的多个地方使用一个占位符的唯一方法吗?

                  解决方案

                  PDO::prepare 表示

                  <块引用>

                  [y]你不能在准备好的语句中多次使用同名的命名参数标记,除非模拟模式打开.

                  由于通常最好关闭仿真模式(因此数据库执行准备好的语句),因此您必须使用 id_0id_1 等>

                  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?

                  解决方案

                  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.

                  这篇关于PHP 的 PDO 准备语句:我可以多次使用一个占位符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP sqlsrv 驱动程序和 PDO 驱动程序之间的数据类型 下一篇:PDO bindParam 问题

                  相关文章

                  最新文章

                  <small id='SFdDD'></small><noframes id='SFdDD'>

                    <bdo id='SFdDD'></bdo><ul id='SFdDD'></ul>
                  <i id='SFdDD'><tr id='SFdDD'><dt id='SFdDD'><q id='SFdDD'><span id='SFdDD'><b id='SFdDD'><form id='SFdDD'><ins id='SFdDD'></ins><ul id='SFdDD'></ul><sub id='SFdDD'></sub></form><legend id='SFdDD'></legend><bdo id='SFdDD'><pre id='SFdDD'><center id='SFdDD'></center></pre></bdo></b><th id='SFdDD'></th></span></q></dt></tr></i><div id='SFdDD'><tfoot id='SFdDD'></tfoot><dl id='SFdDD'><fieldset id='SFdDD'></fieldset></dl></div>

                      <legend id='SFdDD'><style id='SFdDD'><dir id='SFdDD'><q id='SFdDD'></q></dir></style></legend>

                    1. <tfoot id='SFdDD'></tfoot>