pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<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 class="pzr5rl5" 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'>

      1. PDO 多個(gè)命名占位符不檢索數(shù)據(jù)

        PDO multiple named placeholders doesnt retrieve data(PDO 多個(gè)命名占位符不檢索數(shù)據(jù))

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

              <tbody id='oqIf6'></tbody>
            <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 class="bzf5blh" 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>
            • <tfoot id='oqIf6'></tfoot>

                • 本文介紹了PDO 多個(gè)命名占位符不檢索數(shù)據(jù)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  到目前為止,如果準(zhǔn)備好的語(yǔ)句只有一個(gè)命名占位符,我編寫的代碼可以正常工作,但如果查詢有多個(gè)條件,則不會(huì)從數(shù)據(jù)庫(kù)返回任何結(jié)果.

                  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參數(shù))盡管mySQL數(shù)據(jù)庫(kù)中有滿足條件的數(shù)據(jù),但它沒有返回任何內(nèi)容.

                  (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);
                  

                  我正在動(dòng)態(tài)構(gòu)建查詢,因此我通過(guò)去除句點(diǎn)和下劃線來(lái)清理占位符 - 因此使用了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.

                  正在生成的查詢?nèi)缦滤?

                  The query being generated looks like this:

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

                  被綁定的參數(shù)如下所示:

                  And the parameters being bound look like this:

                  eslcommentscommentVisible - Y
                  eslcommentscommentID - 1
                  

                  推薦答案

                  bindParam 需要參考

                  問(wèn)題是你在foreach循環(huán)中綁定參數(shù)的方式造成的.

                  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 需要引用.它將變量而不是值綁定到語(yǔ)句.由于 foreach 循環(huán)中的變量在每次迭代開始時(shí)重置,因此只有對(duì) $bound 的最后一個(gè)引用保持不變,并且您最終將所有占位符綁定到它.

                  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'] 僅包含一個(gè)條目時(shí)可以工作,但在包含多個(gè)條目時(shí)失敗的原因.

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

                  您可以通過(guò)兩種方式解決問(wèn)題:

                  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
                  }
                  

                  這篇關(guān)于PDO 多個(gè)命名占位符不檢索數(shù)據(jù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準(zhǔn)備好的語(yǔ)句amp;foreach 循環(huán))
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個(gè)服務(wù)器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無(wú)法識(shí)別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個(gè)參數(shù))
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結(jié)果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“l(fā)ocalhost的訪問(wèn)被拒絕)

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

                        • <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 class="7hnzrxx" id='usTFr'><tfoot id='usTFr'></tfoot><dl id='usTFr'><fieldset id='usTFr'></fieldset></dl></div>
                        • <tfoot id='usTFr'></tfoot>

                              <tbody id='usTFr'></tbody>
                          • <small id='usTFr'></small><noframes id='usTFr'>

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

                            主站蜘蛛池模板: 神超官网_焊接圆锯片_高速钢锯片_硬质合金锯片_浙江神超锯业制造有限公司 | 橡胶粉碎机_橡胶磨粉机_轮胎粉碎机_轮胎磨粉机-河南鼎聚重工机械制造有限公司 | 艺术漆十大品牌_艺术涂料加盟代理_蒙太奇艺术涂料厂家品牌|艺术漆|微水泥|硅藻泥|乳胶漆 | 成都装修公司-成都装修设计公司推荐-成都朗煜装饰公司 | 钢格栅板_钢格板网_格栅板-做专业的热镀锌钢格栅板厂家-安平县迎瑞丝网制造有限公司 | 代写标书-专业代做标书-商业计划书代写「深圳卓越创兴公司」 | 宠物店加盟_宠物连锁店_开宠物店-【派多格宠物】 | 垃圾处理设备_餐厨垃圾处理设备_厨余垃圾处理设备_果蔬垃圾处理设备-深圳市三盛环保科技有限公司 | 螺旋压榨机-刮泥机-潜水搅拌机-电动泥斗-潜水推流器-南京格林兰环保设备有限公司 | 北京办公室装修,办公室设计,写字楼装修-北京金视觉装饰工程公司 北京成考网-北京成人高考网 | 金蝶帐无忧|云代账软件|智能财税软件|会计代账公司专用软件 | 拉伸膜,PE缠绕膜,打包带,封箱胶带,包装膜厂家-东莞宏展包装 | 东莞海恒试验仪器设备有限公司| Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 右手官网|右手工业设计|外观设计公司|工业设计公司|产品创新设计|医疗产品结构设计|EMC产品结构设计 | 黑龙江「京科脑康」医院-哈尔滨失眠医院_哈尔滨治疗抑郁症医院_哈尔滨精神心理医院 | 淄博不锈钢,淄博不锈钢管,淄博不锈钢板-山东振远合金科技有限公司 | 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | 深圳宣传片制作-企业宣传视频制作-产品视频拍摄-产品动画制作-短视频拍摄制作公司 | 户外环保不锈钢垃圾桶_标识标牌制作_园林公园椅厂家_花箱定制-北京汇众环艺 | 玉米深加工设备|玉米加工机械|玉米加工设备|玉米深加工机械-河南成立粮油机械有限公司 | 办公室家具_板式办公家具定制厂家-FMARTS福玛仕办公家具 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 偏心半球阀-电动偏心半球阀-调流调压阀-旋球阀-上欧阀门有限公司 | 石牌坊价格石牌坊雕刻制作_石雕牌坊牌楼石栏杆厂家_山东嘉祥石雕有限公司 | 消防泵-XBD单级卧式/立式消防泵-上海塑泉泵阀(集团)有限公司 | 山东石英砂过滤器,除氟过滤器「价格低」-淄博胜达水处理 | 青岛侦探_青岛侦探事务所_青岛劝退小三_青岛婚外情取证-青岛王军侦探事务所 | 西门子伺服电机维修,西门子电源模块维修,西门子驱动模块维修-上海渠利 | 滚塑PE壳体-PE塑料浮球-警示PE浮筒-宁波君益塑业有限公司 | 聚合氯化铝-碱式氯化铝-聚合硫酸铁-聚氯化铝铁生产厂家多少钱一吨-聚丙烯酰胺价格_河南浩博净水材料有限公司 | 糖衣机,除尘式糖衣机,全自动糖衣机,泰州市长江制药机械有限公司 体感VRAR全息沉浸式3D投影多媒体展厅展会游戏互动-万展互动 | 顶呱呱交易平台-行业领先的公司资产交易服务平台 | 喷码机,激光喷码打码机,鸡蛋打码机,手持打码机,自动喷码机,一物一码防伪溯源-恒欣瑞达有限公司 | 卓能JOINTLEAN端子连接器厂家-专业提供PCB接线端子|轨道式端子|重载连接器|欧式连接器等电气连接产品和服务 | 柔性测斜仪_滑动测斜仪-广州杰芯科技有限公司 | EPDM密封胶条-EPDM密封垫片-EPDM生产厂家 | 至顶网| 工业洗衣机_工业洗涤设备_上海力净工业洗衣机厂家-洗涤设备首页 bkzzy在职研究生网 - 在职研究生招生信息咨询平台 | 升降炉_真空气氛炉_管式电阻炉厂家-山东中辰电炉有限公司 |