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

        <tfoot id='iIDcG'></tfoot>

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

        参数化 PDO 查询和 `LIMIT` 子句 - 不工作

        时间:2023-09-21

          1. <legend id='0enpD'><style id='0enpD'><dir id='0enpD'><q id='0enpD'></q></dir></style></legend>

              <bdo id='0enpD'></bdo><ul id='0enpD'></ul>

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

                  本文介绍了参数化 PDO 查询和 `LIMIT` 子句 - 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有这样的查询:

                  SELECT imageurl 
                  FROM entries 
                  WHERE thumbdl IS NULL 
                  LIMIT 10;
                  

                  它与 PDO 和 MySQL Workbench 完美配合(它根据我的需要返回 10 个网址).

                  It works perfectly with PDO and MySQL Workbench (it returns 10 urls as I want).

                  但是我尝试使用 PDO 参数化 LIMIT:

                  However I tried to parametrize LIMIT with PDO:

                  $cnt = 10;
                  $query = $this->link->prepare("
                               SELECT imageurl 
                               FROM entries 
                               WHERE imgdl is null 
                               LIMIT ?
                           ");
                  
                  $query->bindValue(1, $cnt);
                  
                  $query->execute();
                  
                  $result = $query->fetchAll(PDO::FETCH_ASSOC);
                  

                  返回空数组.

                  推荐答案

                  我刚刚测试了一堆案例.我在 OS X 上使用 PHP 5.3.15,并查询 MySQL 5.6.12.

                  I just tested a bunch of cases. I'm using PHP 5.3.15 on OS X, and querying MySQL 5.6.12.

                  如果您设置了任何组合:

                  Any combination works if you set:

                  $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
                  

                  以下所有工作:您可以使用 int 或字符串;你不需要使用 PDO::PARAM_INT.

                  All of the following work: you can use either an int or a string; you don't need to use PDO::PARAM_INT.

                  $stmt = $dbh->prepare("select user from mysql.user limit ?");
                  
                  $int = intval(1);
                  $int = '1';
                  
                  $stmt->bindValue(1, 1);
                  $stmt->execute();
                  print_r($stmt->fetchAll());
                  
                  $stmt->bindValue(1, '1');
                  $stmt->execute();
                  print_r($stmt->fetchAll());
                  
                  $stmt->bindValue(1, 1, PDO::PARAM_INT);
                  $stmt->execute();
                  print_r($stmt->fetchAll());
                  
                  $stmt->bindValue(1, '1', PDO::PARAM_INT);
                  $stmt->execute();
                  print_r($stmt->fetchAll());
                  
                  $stmt->bindParam(1, $int);
                  $stmt->execute();
                  print_r($stmt->fetchAll());
                  
                  $stmt->bindParam(1, $string);
                  $stmt->execute();
                  print_r($stmt->fetchAll());
                  
                  $stmt->bindParam(1, $int, PDO::PARAM_INT);
                  $stmt->execute();
                  print_r($stmt->fetchAll());
                  
                  $stmt->bindParam(1, $string, PDO::PARAM_INT);
                  $stmt->execute();
                  print_r($stmt->fetchAll());
                  

                  您也可以忘记 bindValue() 或 bindParam(),而是将数组参数中的 int 或字符串传递给 execute().这工作正常并且做同样的事情,但使用数组更简单,通常更方便编码.

                  You can also forget about bindValue() or bindParam(), and instead pass either an int or a string in an array argument to execute(). This works fine and does the same thing, but using an array is simpler and often more convenient to code.

                  $stmt = $dbh->prepare("select user from mysql.user limit ?");
                  
                  $stmt->execute(array($int));
                  print_r($stmt->fetchAll());
                  
                  $stmt->execute(array($string));
                  print_r($stmt->fetchAll());
                  

                  如果您启用模拟准备,则只有一种组合有效:您必须使用整数作为参数并且您必须指定 PDO::PARAM_INT:

                  If you enable emulated prepares, only one combination works: you must use an integer as the parameter and you must specify PDO::PARAM_INT:

                  $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
                  
                  $stmt = $dbh->prepare("select user from mysql.user limit ?");
                  
                  $stmt->bindValue(1, $int, PDO::PARAM_INT);
                  $stmt->execute();
                  print_r($stmt->fetchAll());
                  
                  $stmt->bindParam(1, $int, PDO::PARAM_INT);
                  $stmt->execute();
                  print_r($stmt->fetchAll());
                  

                  如果您启用了模拟准备,则无法将值传递给 execute().

                  Passing values to execute() doesn't work if you have emulated prepares enabled.

                  这篇关于参数化 PDO 查询和 `LIMIT` 子句 - 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何读取“fetch(PDO::FETCH_ASSOC);" 下一篇:在 ZendFramework 中,如何让 PDO 每次连接时都运行

                  相关文章

                  最新文章

                2. <tfoot id='Tqx6m'></tfoot>

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

                    • <bdo id='Tqx6m'></bdo><ul id='Tqx6m'></ul>

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