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

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

      1. <legend id='tByHM'><style id='tByHM'><dir id='tByHM'><q id='tByHM'></q></dir></style></legend>
      2. <tfoot id='tByHM'></tfoot>
        • <bdo id='tByHM'></bdo><ul id='tByHM'></ul>

        使用命名占位符设置 PDO/MySQL LIMIT

        时间:2023-10-05

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

              • <bdo id='YcOy9'></bdo><ul id='YcOy9'></ul>
                  <tbody id='YcOy9'></tbody>

                  <tfoot id='YcOy9'></tfoot>
                1. 本文介绍了使用命名占位符设置 PDO/MySQL LIMIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在绑定 SQL 查询的 LIMIT 部分时遇到问题.这是因为查询是作为字符串传递的.我在这里看到了 another Q 这里涉及绑定参数,没有涉及 Named数组中的占位符.

                  I'm having an issue binding the LIMIT part of an SQL query. This is because the query is being passed as a string. I've seen another Q here that deals with binding parameters, nothing that deals with Named Placeholders in an array.

                  这是我的代码:

                  public function getLatestWork($numberOfSlides, $type = 0) {
                  
                  $params = array();
                  $params["numberOfSlides"] = (int) trim($numberOfSlides);
                  $params["type"] = $type;
                  
                  $STH = $this->_db->prepare("SELECT slideID 
                      FROM slides
                      WHERE visible = 'true'
                          AND type = :type
                      ORDER BY order
                      LIMIT :numberOfSlides;");
                  
                  $STH->execute($params);
                  
                  $result = $STH->fetchAll(PDO::FETCH_COLUMN);
                  
                  return $result;        
                  }
                  

                  我得到的错误是:'20''附近的语法错误或访问冲突(20 是 $numberOfSlides 的值).

                  The error I'm getting is: Syntax error or access violation near ''20'' (20 is the value of $numberOfSlides).

                  我该如何解决这个问题?

                  How can I fix this?

                  推荐答案

                  问题在于 execute() 引用了数字 并将其视为字符串:

                  The problem is that execute() quotes the numbers and treats as strings:

                  来自手册 - 一个值数组,其元素与正在执行的 SQL 语句中的绑定参数一样多.所有值都被视为 PDO::PARAM_STR.

                  From the manual - An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR.

                  <?php 
                  public function getLatestWork($numberOfSlides=10, $type=0) {
                  
                      $numberOfSlides = intval(trim($numberOfSlides));
                  
                      $STH = $this->_db->prepare("SELECT slideID
                                                  FROM slides
                                                  WHERE visible = 'true'
                                                  AND type = :type
                                                  ORDER BY order
                                                  LIMIT :numberOfSlides;");
                  
                      $STH->bindParam(':numberOfSlides', $numberOfSlides, PDO::PARAM_INT);
                      $STH->bindParam(':type', $type, PDO::PARAM_INT);
                  
                      $STH->execute();
                      $result = $STH->fetchAll(PDO::FETCH_COLUMN);
                  
                      return $result;
                  }
                  ?>
                  

                  这篇关于使用命名占位符设置 PDO/MySQL LIMIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP PDO 准备好的语句查询不更新记录 下一篇:PDO 'LIKE' 查询

                  相关文章

                  最新文章

                    <bdo id='W8mp0'></bdo><ul id='W8mp0'></ul>

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

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