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

  • <small id='g0T4n'></small><noframes id='g0T4n'>

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

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

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

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

      1. 簡單的 PDO 包裝器

        Simple PDO wrapper(簡單的 PDO 包裝器)
          <tbody id='m72e1'></tbody>
        <i id='m72e1'><tr id='m72e1'><dt id='m72e1'><q id='m72e1'><span id='m72e1'><b id='m72e1'><form id='m72e1'><ins id='m72e1'></ins><ul id='m72e1'></ul><sub id='m72e1'></sub></form><legend id='m72e1'></legend><bdo id='m72e1'><pre id='m72e1'><center id='m72e1'></center></pre></bdo></b><th id='m72e1'></th></span></q></dt></tr></i><div class="skueock" id='m72e1'><tfoot id='m72e1'></tfoot><dl id='m72e1'><fieldset id='m72e1'></fieldset></dl></div>

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

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

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

                  <legend id='m72e1'><style id='m72e1'><dir id='m72e1'><q id='m72e1'></q></dir></style></legend>
                • 本文介紹了簡單的 PDO 包裝器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我的 Web 應用程序目前確實執行了簡單的查詢:簡單的 CRUD 操作、計數、...

                  My web application currently has do execute simple queries: simple CRUD operations, counting,...

                  幾個月前,有人推薦我在這里為此編寫一個簡單的 PDO 包裝器(以避免每次應執行查詢時編寫 try/catch、prepare()、execute() 等).顯示了此示例方法(我進行了一些更改,以便可以在我自己的項目中使用它):

                  A few months ago, someone recommended me here to write a simple PDO wrapper for this (to avoid writing try/catch, prepare(), execute(), etc. each time a query should be executed). This example method was shown (I've made some changes so I could use it in my own project):

                  public function execute() {
                      $args  = func_get_args();
                      $query = array_shift($args);
                      $result = false;
                  
                      try {
                        $res = $this->pdo->prepare($query);
                        $result = $res->execute($args);
                      } catch (PDOException $e) { echo $e->getMessage(); }
                  
                      return $result;
                    }
                  

                  由于我需要執行更多操作(執行查詢、檢索 1 條記錄、檢索多條記錄、計算結果),我為所有這些操作創建了一個方法:

                  As I need to perform more operations (executing queries, retrieving 1 record, retrieving multiple records, counting results) I created a method for all of these:

                    public function getMultipleRecords() {
                      $args  = func_get_args();
                      $query = array_shift($args);
                      $records = array();
                  
                      try {
                        $res = $this->pdo->prepare($query);
                        $res->execute($args);
                        $records = $res->fetchAll();
                      } catch (PDOException $e) { echo $e->getMessage(); }
                  
                      return $records;
                    }
                  
                    public function getSingleRecord() {
                      $args  = func_get_args();
                      $query = array_shift($args);
                      $record = array();
                  
                      try {
                        $res = $this->pdo->prepare($query);
                        $res->execute($args);
                        $record = $res->fetch();
                      } catch (PDOException $e) { echo $e->getMessage(); }
                  
                      return $record;
                    }
                  
                    public function execute() {
                      $args  = func_get_args();
                      $query = array_shift($args);
                      $result = false;
                  
                      try {
                        $res = $this->pdo->prepare($query);
                        $result = $res->execute($args);
                      } catch (PDOException $e) { echo $e->getMessage(); }
                  
                      return $result;
                    }
                  
                    public function count() {
                      $args  = func_get_args();
                      $query = array_shift($args);
                      $result = -1;
                  
                      try {
                        $res = $this->pdo->prepare($query);
                        $res->execute($args);
                        $result = $res->fetchColumn();
                      } catch(PDOException $e) { echo $e->getMessage(); }
                  
                      return $result;
                    }
                  

                  如您所見,大部分代碼是相同的.每種方法只有 2 行代碼不同:$result 的初始化(我總是想返回一個值,即使查詢失敗)和獲取.我可以不使用 4 種方法,而是只編寫其中一種并傳遞一個帶有操作類型的額外參數.這樣,我可以使用 switch 語句的一堆 if/else 語句.但是,我認為代碼可能會變得混亂.這是解決這個問題的好方法嗎?如果沒有,有什么好的解決辦法?

                  As you see, most of the code is the same. Only 2 lines of code are different for each method: the initialisation of $result (I always want to return a value, even if the query fails) and the fetching. Instead of using 4 methods, I could write just one of them and pass an extra parameter with the type of action. That way, I could use a bunch of if/else statements of a switch statement. However, I think the code can get messy. Is this a good way for solving this problem? If not, what would be a good solution to it?

                  我遇到的第二個問題(這就是我現在正在研究這個類的原因)是我想將準備好的語句與 LIMIT SQL 語句一起使用.但是,這是不可能的:

                  The second problem I have (which is why I'm working on this class right now) is that I want to use prepared statements with the LIMIT SQL statement. However, it is not possible to do this:

                  $res = $pdo->prepare("SELECT * FROM table LIMIT ?");
                  $res->execute(array($int));
                  

                  由于某種原因,變量將被引用(因此查詢將失敗),如下所述:https://bugs.php.net/bug.php?id=40740

                  The variabele will be quoted for some reason (and so the query will fail), as explained here: https://bugs.php.net/bug.php?id=40740

                  解決方案似乎使用 bindValue() 并使用 int 數據類型作為參數:http://www.php.net/manual/de/pdostatement.bindvalue.php

                  The solution seems to use bindValue() and use the int datatype as a parameter: http://www.php.net/manual/de/pdostatement.bindvalue.php

                  我可以重寫方法來支持這一點,但我還需要使用一個額外的參數.我不能再使用 $db->e??xecute($sql, $variable1, $variable2); 因為我需要知道數據類型.

                  I could rewrite the method(s) to support this, but I would also need to use an extra parameter. I can't just use $db->execute($sql, $variable1, $variable2); anymore as I need to know the data type.

                  解決這個問題的最佳方法是什么?

                  What's the best way to solve this?

                  謝謝

                  推薦答案

                  如何使用可以鏈接的方法創建一個類(為了清楚起見,我已經刪除了錯誤檢查):

                  How about creating a class with methods that you can chain (for clarity, I've removed error checking):

                  class DB {
                  
                      private $dbh;
                      private $stmt;
                  
                      public function __construct($user, $pass, $dbname) {
                          $this->dbh = new PDO(
                              "mysql:host=localhost;dbname=$dbname",
                              $user,
                              $pass,
                              array( PDO::ATTR_PERSISTENT => true )
                          );
                      }
                  
                      public function query($query) {
                          $this->stmt = $this->dbh->prepare($query);
                          return $this;
                      }
                  
                      public function bind($pos, $value, $type = null) {
                  
                          if( is_null($type) ) {
                              switch( true ) {
                                  case is_int($value):
                                      $type = PDO::PARAM_INT;
                                      break;
                                  case is_bool($value):
                                      $type = PDO::PARAM_BOOL;
                                      break;
                                  case is_null($value):
                                      $type = PDO::PARAM_NULL;
                                      break;
                                  default:
                                      $type = PDO::PARAM_STR;
                              }
                          }
                  
                          $this->stmt->bindValue($pos, $value, $type);
                          return $this;
                      }
                  
                      public function execute() {
                          return $this->stmt->execute();
                      }
                  
                      public function resultset() {
                          $this->execute();
                          return $this->stmt->fetchAll();
                      }
                  
                      public function single() {
                          $this->execute();
                          return $this->stmt->fetch();
                      }
                  }
                  

                  然后您可以像這樣使用它:

                  You can then use it like this:

                  // Establish a connection.
                  $db = new DB('user', 'password', 'database');
                  
                  // Create query, bind values and return a single row.
                  $row = $db->query('SELECT col1, col2, col3 FROM mytable WHERE id > ? LIMIT ?')
                     ->bind(1, 2)
                     ->bind(2, 1)
                     ->single();
                  
                  // Update the LIMIT and get a resultset.
                  $db->bind(2,2);
                  $rs = $db->resultset();
                  
                  // Create a new query, bind values and return a resultset.
                  $rs = $db->query('SELECT col1, col2, col3 FROM mytable WHERE col2 = ?')
                     ->bind(1, 'abc')
                     ->resultset();
                  
                  // Update WHERE clause and return a resultset.
                  $db->bind(1, 'def');
                  $rs = $db->resultset();
                  

                  如果您愿意,您可以更改 bind 方法以接受數組或關聯數組,但我發現此語法非常清晰 - 它避免了必須構建數組.參數類型檢查是可選的,因為 PDO::PARAM_STR 適用于大多數值,但請注意傳遞空值時的潛在問題(請參閱 PDOStatement->bindValue 文檔中的注釋).

                  You could alter the bind method to accept an array or associative array if you prefer, but I find this syntax quite clear - it avoids having to build an array. The parameter type checking is optional, as PDO::PARAM_STR works for most values, but be aware of potential issues when passing null values (see comment in PDOStatement->bindValue documentation).

                  這篇關于簡單的 PDO 包裝器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)
                  <i id='zqML4'><tr id='zqML4'><dt id='zqML4'><q id='zqML4'><span id='zqML4'><b id='zqML4'><form id='zqML4'><ins id='zqML4'></ins><ul id='zqML4'></ul><sub id='zqML4'></sub></form><legend id='zqML4'></legend><bdo id='zqML4'><pre id='zqML4'><center id='zqML4'></center></pre></bdo></b><th id='zqML4'></th></span></q></dt></tr></i><div class="4siwaw6" id='zqML4'><tfoot id='zqML4'></tfoot><dl id='zqML4'><fieldset id='zqML4'></fieldset></dl></div>

                  1. <small id='zqML4'></small><noframes id='zqML4'>

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

                        • <legend id='zqML4'><style id='zqML4'><dir id='zqML4'><q id='zqML4'></q></dir></style></legend>
                            主站蜘蛛池模板: 无菌检查集菌仪,微生物限度仪器-苏州长留仪器百科 | 化工ERP软件_化工新材料ERP系统_化工新材料MES软件_MES系统-广东顺景软件科技有限公司 | 东莞精密模具加工,精密连接器模具零件,自動機零件,冶工具加工-益久精密 | 深圳网站建设-高端企业网站开发-定制网页设计制作公司 | ★店家乐|服装销售管理软件|服装店收银系统|内衣店鞋店进销存软件|连锁店管理软件|收银软件手机版|会员管理系统-手机版,云版,App | 滚塑PE壳体-PE塑料浮球-警示PE浮筒-宁波君益塑业有限公司 | 包头市鑫枫装饰有限公司| 高通量组织研磨仪-多样品组织研磨仪-全自动组织研磨仪-研磨者科技(广州)有限公司 | 鼓风干燥箱_真空烘箱_高温干燥箱_恒温培养箱-上海笃特科学仪器 | 广州展台特装搭建商|特装展位设计搭建|展会特装搭建|特装展台制作设计|展览特装公司 | 北京自然绿环境科技发展有限公司专业生产【洗车机_加油站洗车机-全自动洗车机】 | 篮球地板厂家_舞台木地板品牌_体育运动地板厂家_凯洁地板 | 金属清洗剂,防锈油,切削液,磨削液-青岛朗力防锈材料有限公司 | 苏州防水公司_厂房屋面外墙防水_地下室卫生间防水堵漏-苏州伊诺尔防水工程有限公司 | 喷播机厂家_二手喷播机租赁_水泥浆洒布机-河南青山绿水机电设备有限公司 | 钢化玻璃膜|手机钢化膜|钢化膜厂家|手机保护膜-【东莞市大象电子科技有限公司】 | 阴离子_阳离子聚丙烯酰胺厂家_聚合氯化铝价格_水处理絮凝剂_巩义市江源净水材料有限公司 | nalgene洗瓶,nalgene量筒,nalgene窄口瓶,nalgene放水口大瓶,浙江省nalgene代理-杭州雷琪实验器材有限公司 | 上海电子秤厂家,电子秤厂家价格,上海吊秤厂家,吊秤供应价格-上海佳宜电子科技有限公司 | 定硫仪,量热仪,工业分析仪,马弗炉,煤炭化验设备厂家,煤质化验仪器,焦炭化验设备鹤壁大德煤质工业分析仪,氟氯测定仪 | 深圳希玛林顺潮眼科医院(官网)│深圳眼科医院│医保定点│香港希玛林顺潮眼科中心连锁品牌 | 武汉森源蓝天环境科技工程有限公司-为环境污染治理提供协同解决方案 | 南汇8424西瓜_南汇玉菇甜瓜-南汇水蜜桃价格| 外贸网站建设-外贸网站设计制作开发公司-外贸独立站建设【企术】 | 工业风机_环保空调_冷风机_工厂车间厂房通风降温设备旺成服务平台 | 课件导航网_ppt课件_课件模板_课件下载_最新课件资源分享发布平台 | 硫化罐-电加热蒸汽硫化罐生产厂家-山东鑫泰鑫智能装备有限公司 | 金环宇|金环宇电线|金环宇电缆|金环宇电线电缆|深圳市金环宇电线电缆有限公司|金环宇电缆集团 | 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 | 杭州代理记账多少钱-注册公司代办-公司注销流程及费用-杭州福道财务管理咨询有限公司 | 首页|专注深圳注册公司,代理记账报税,注册商标代理,工商变更,企业400电话等企业一站式服务-慧用心 | 甲级防雷检测仪-乙级防雷检测仪厂家-上海胜绪电气有限公司 | 恒温槽_恒温水槽_恒温水浴槽-上海方瑞仪器有限公司 | 纸塑分离机-纸塑分离清洗机设备-压力筛-碎浆机厂家金双联环保 | 粉末包装机-给袋式包装机-全自动包装机-颗粒-液体-食品-酱腌菜包装机生产线【润立机械】 | 进口便携式天平,外校_十万分之一分析天平,奥豪斯工业台秤,V2000防水秤-重庆珂偌德科技有限公司(www.crdkj.com) | 深圳市宏康仪器科技有限公司-模拟高空低压试验箱-高温防爆试验箱-温控短路试验箱【官网】 | 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | 膜结构_ETFE膜结构_膜结构厂家_膜结构设计-深圳市烨兴智能空间技术有限公司 | 高压包-点火器-高压发生器-点火变压器-江苏天网 | 小区健身器材_户外健身器材_室外健身器材_公园健身路径-沧州浩然体育器材有限公司 |