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

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

      2. PDO 多个命名占位符不检索数据

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

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

                  <tbody id='kUp1o'></tbody>

                  本文介绍了PDO 多个命名占位符不检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  到目前为止,如果准备好的语句只有一个命名占位符,我编写的代码可以正常工作,但如果查询有多个条件,则不会从数据库返回任何结果.

                  The code I've written so far works fine if there is only one named place holder for a prepared statement but if there are multiple conditions for a query, it doesn't return any results from the database.

                  例如:

                  $query = array();
                  $query['columns'] = array('*');
                  $query['tables'] = array('esl_comments');
                  $query['where'] = array(
                      'esl_comments.commentVisible' => array('=', 'Y')
                  );
                  

                  工作正常.但如果我尝试:

                  Works fine. But if I try:

                  $query = array();
                  $query['columns'] = array('*');
                  $query['tables'] = array('esl_comments');
                  $query['where'] = array(
                      'esl_comments.commentVisible' => array('=', 'Y'),
                      'esl_comments.commentID' => array('=', '1'),
                  );
                  

                  (注意附加的commentID参数)尽管mySQL数据库中有满足条件的数据,但它没有返回任何内容.

                  (Note the additional commentID parameter) it fails to return anything despite there being data in the mySQL database that satisfies the conditions.

                  我编写的 PDO 代码是:

                  The PDO code i've written is:

                  $sql ='SELECT ';
                                  foreach($query['columns'] as $column){ //What columnns do we want to fetch?
                                      $sql.=$column . ", ";
                                  }
                                  $sql = rtrim($sql, " ,");
                                  $sql .=' FROM '; //Which tables will we be accessing?
                                  foreach($query['tables'] as $tables){
                                      $sql.=$tables . ", ";
                                  }
                                  $sql = rtrim($sql, " ,"); //Get rid of the last comma
                                  $sql .=' WHERE ';
                  
                                  if(array_key_exists('where', $query)) //check if a where clause was provided
                                  {
                                      $fieldnames = array_keys($query['where']);
                                      $count = 0;
                                      $size = sizeof($fieldnames);
                                      $bindings = array();
                                      foreach($query['where'] as $where){
                  
                                          $cleanPlaceholder = str_replace("_", "", $fieldnames[$count]);
                                          $cleanPlaceholder = str_replace(".", "", $cleanPlaceholder);
                                          $sql.=$fieldnames[$count].$where[0].":".$cleanPlaceholder." AND ";
                                          $bindings[$cleanPlaceholder]=$where[1];
                                          $count++;
                                      }
                                      $sql = substr($sql, 0, -5);  //Remove the last AND
                                  }
                                  else{ //no where clause so set it to an always true check
                                      $sql.='1=1';
                                      $bindings=array('1'=>'1'); //Provide default bindings for the statement
                                  }
                  
                                  $sql .= ';'; //Add the semi-colon to note the end of the query
                                  echo $sql . "<br/><br/>";
                              //  exit();
                                  $stmt = $this->_connection->prepare($sql);
                  
                                  foreach($bindings as $placeholder=>$bound){
                                      echo $placeholder . " - " . $bound."<br/>";
                                      $stmt->bindParam($placeholder, $bound);
                                  }
                  
                                  $result = $stmt->execute();
                                  echo $stmt->rowCount() . " records<br/>";
                  
                                  $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
                  

                  我正在动态构建查询,因此我通过去除句点和下划线来清理占位符 - 因此使用了cleanPlaceholder"变量.

                  I'm building queries dynamically and therefore I am cleaning the placeholders, by stripping them of periods and underscores - hence the use of the 'cleanPlaceholder' variable.

                  正在生成的查询如下所示:

                  The query being generated looks like this:

                  SELECT * FROM esl_comments WHERE esl_comments.commentVisible=:eslcommentscommentVisible AND esl_comments.commentID=:eslcommentscommentID;
                  

                  被绑定的参数如下所示:

                  And the parameters being bound look like this:

                  eslcommentscommentVisible - Y
                  eslcommentscommentID - 1
                  

                  推荐答案

                  bindParam 需要参考

                  问题是你在foreach循环中绑定参数的方式造成的.

                  bindParam Requires a reference

                  The problem is caused by the way you bind parameters in the foreach loop.

                  foreach($bindings as $placeholder=>$bound){
                      echo $placeholder . " - " . $bound."<br/>";
                      $stmt->bindParam($placeholder, $bound);
                  }
                  

                  bindParam 需要引用.它将变量而不是值绑定到语句.由于 foreach 循环中的变量在每次迭代开始时重置,因此只有对 $bound 的最后一个引用保持不变,并且您最终将所有占位符绑定到它.

                  bindParam requires a reference. It binds the variable, not the value, to the statement. Since the variable in a foreach loop is reset at the start of each iteration, only the last reference to $bound is left intact, and you end up binding all your placeholders to it.

                  这就是为什么您的代码在 $query['where'] 仅包含一个条目时可以工作,但在包含多个条目时失败的原因.

                  That's why your code works when $query['where'] contains only one entry, but fails when it contains more than one.

                  您可以通过两种方式解决问题:

                  You can solve the problem in 2 ways:

                  foreach($bindings as $placeholder => &$bound) {  //pass $bound as a reference (&)
                      $stmt->bindParam($placeholder, $bound);     // bind the variable to the statement
                  }
                  

                  传值

                  使用bindValue代替bindParam:

                  foreach($bindings as $placeholder => $bound) {  
                      $stmt->bindValue($placeholder, $bound);     // bind the value to the statement
                  }
                  

                  这篇关于PDO 多个命名占位符不检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PDO 多次获取同一查询 下一篇:PHP Mysql PDO 绑定变量数量与令牌数量不匹配

                  相关文章

                  最新文章

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

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

                    3. <small id='usTFr'></small><noframes id='usTFr'>

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