久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <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="qwawimqqmiuu" 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="qwawimqqmiuu" 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="qwawimqqmiuu" 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>
                            主站蜘蛛池模板: 国产精品一区二区久久 | 先锋av资源在线 | 亚洲男人天堂 | 久久亚洲精品久久国产一区二区 | 最新国产精品精品视频 | 亚洲a毛片 | 亚洲一区在线播放 | 亚洲精品久久久久久国产精华液 | av小说在线 | 国产激情一区二区三区 | 噜久寡妇噜噜久久寡妇 | 成人网av| 日韩电影在线一区 | 精品视频在线观看 | 三级视频在线观看 | av国产精品毛片一区二区小说 | 成人国产精品免费观看 | 四虎成人免费视频 | 美日韩精品 | 亚洲精品白浆高清久久久久久 | 少妇一级淫片免费播放 | 国产精品美女久久久免费 | 欧美电影在线 | 欧美日韩国产高清 | avmans最新导航地址 | 综合精品 | 天天操妹子 | 亚洲精品久久久久久久久久久久久 | 武道仙尊动漫在线观看 | 日韩1区| 日本久久久久久久久 | 91av国产在线视频 | 精品一区二区三区在线观看 | 久久精彩视频 | 日韩视频一级 | 成人免费影院 | 在线视频91 | 秋霞影院一区二区 | 日本精品一区二区三区在线观看视频 | 欧美一区二区成人 | 中文字幕亚洲欧美日韩在线不卡 |