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

    1. <legend id='Wbzwq'><style id='Wbzwq'><dir id='Wbzwq'><q id='Wbzwq'></q></dir></style></legend>
      • <bdo id='Wbzwq'></bdo><ul id='Wbzwq'></ul>

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

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

      1. 擴展 PDO 類

        extending PDO class(擴展 PDO 類)

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

            <tbody id='3uzuX'></tbody>
          <legend id='3uzuX'><style id='3uzuX'><dir id='3uzuX'><q id='3uzuX'></q></dir></style></legend>

          1. <tfoot id='3uzuX'></tfoot>
            • <small id='3uzuX'></small><noframes id='3uzuX'>

                  <bdo id='3uzuX'></bdo><ul id='3uzuX'></ul>
                  本文介紹了擴展 PDO 類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  以下是我目前推出的數據庫連接類,但我將通過擴展 PDO 類本身來改進它,

                  Below is the db connection class I came out with so far, but I am going to improve it by extending the PDO class itself,

                  <?php
                  class database
                  {
                      protected $connection = null;
                  
                      #make a connection
                      public function __construct($hostname,$dbname,$username,$password)
                      {
                          try 
                          {
                              # MySQL with PDO_MYSQL  
                              $this->connection = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
                              $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
                          }
                          catch (PDOException $e) 
                          {
                              $this->connection = null;
                              die($e->getMessage());
                          }
                      }
                  
                      #get the number of rows in a result
                      public function num_rows($query)
                      {
                          # create a prepared statement
                          $stmt = $this->connection->prepare($query);
                  
                          if($stmt) 
                          {
                              # execute query 
                              $stmt->execute();
                  
                              return $stmt->rowCount();
                          } 
                          else
                          {
                              return self::get_error();
                          }
                      }
                  
                      #display error
                      public function get_error() 
                      {
                          $this->connection->errorInfo();
                      }
                  
                      # closes the database connection when object is destroyed.
                      public function __destruct()
                      {
                          $this->connection = null;
                      }
                  }
                  ?>
                  

                  擴展類,

                  class database extends PDO
                  {
                  
                      #make a connection
                      public function __construct($hostname,$dbname,$username,$password)
                      {
                          parent::__construct($hostname,$dbname,$username,$password);
                  
                          try 
                          { 
                              $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
                          }
                          catch (PDOException $e) 
                          {
                              die($e->getMessage());
                          }
                      }
                  
                      #get the number of rows in a result
                      public function num_rows($query)
                      {
                          # create a prepared statement
                          $stmt = parent::prepare($query);
                  
                          if($stmt) 
                          {
                              # execute query 
                              $stmt->execute();
                  
                              return $stmt->rowCount();
                          } 
                          else
                          {
                              return self::get_error();
                          }
                      }
                  
                      #display error
                      public function get_error() 
                      {
                          $this->connection->errorInfo();
                      }
                  
                      # closes the database connection when object is destroyed.
                      public function __destruct()
                      {
                          $this->connection = null;
                      }
                  }
                  

                  這就是我實例化類的方式,

                  This is how I instantiate the class,

                  # the host used to access DB
                  define('DB_HOST', 'localhost');
                  
                  # the username used to access DB
                  define('DB_USER', 'root');
                  
                  # the password for the username
                  define('DB_PASS', 'xxx');
                  
                  # the name of your databse 
                  define('DB_NAME', 'db_2011'); 
                  
                  include 'class_database.php';
                  
                  $connection = new database(DB_HOST,DB_NAME,DB_USER,DB_PASS);
                  $sql = "
                      SELECT *
                      FROM root_contacts_cfm
                      ORDER BY cnt_id DESC
                      ";
                  
                  $connection->num_rows($sql);
                  

                  但是當我調用這個擴展的 pdo 類時出現錯誤,

                  But I have errors when I call this extended pdo class,

                  警告:PDO::__construct() 需要參數 4 為數組,給出字符串在 C:wampwwwxxclass_database.php在線 xx

                  Warning: PDO::__construct() expects parameter 4 to be array, string given in C:wampwwwxxclass_database.php on line xx

                  致命錯誤:調用成員函數setAttribute() 在非對象上C:wampwwwxxclass_database.php 上第 xx 行

                  Fatal error: Call to a member function setAttribute() on a non-object in C:wampwwwxxclass_database.php on line xx

                  我在網上做了一些研究,我找到了擴展pdo的這個基本結構,但我不明白......

                  I have done some research online, I found this basic structure of extending pdo but I dont understand it...

                  class myPDO extends PDO
                  {
                     public function __construct($dsn, 
                                                 $username=null, 
                                                 $password=null, 
                                                 $driver_options=null)
                     {
                        parent::__construct($dsn, $username, $password, $driver_options);
                     }
                  
                     public function query($query)
                     {
                        $result = parent::query($query);
                        // do other stuff you want to do here, then...
                        return($result);
                     }
                  }
                  

                  $dsn 變量有什么用?如何將我的 $hostname 變量傳遞給擴展的 pdo 類?

                  What is $dsn variable for? How can I pass my $hostname variable into extended pdo class?

                  另一個問題:如何在擴展的 pdo 類中創建一個顯示錯誤的方法?如何關閉擴展 pdo 類中的連接?

                  Another questions: How can I make a method for displaying error in the extended pdo class? How can I close the connection in the extended pdo class?

                  從mysqli遷移到pdo太難了!

                  It is so difficult to move from mysqli to pdo!

                  謝謝.

                  推薦答案

                  $dsn 是數據源名稱.它為您處理您的主機名.你像這樣使用它:

                  $dsn is data source name. It handles your hostname for you. You use it like this:

                  $dsn = 'mysql:dbname=YOUR_DB_NAME;host=YOUR_HOSTNAME'
                  

                  隨著行 $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 你已經設置了在發生錯誤時引發的異常(我喜歡),所以在你的擴展類,您可以處理異常處理程序中的錯誤.如果您的擴展 PDO 類中有一個名為 getAssoc 的方法,那么它看起來像這樣:

                  With the line $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); You have set exceptions to be raised when errors occur (which I like), so in your extended class you can handle errors in exception handlers. If you had a method called getAssoc in your extended PDO class then it would look like this:

                  /// Get an associative array of results for the sql.
                  public function getAssoc($sql, $params=array())
                  {
                     try
                     {
                        $stmt = $this->prepare($sql);
                        $params = is_array($params) ? $params : array($params);
                        $stmt->execute($params);
                  
                        return $stmt->fetchAll(PDO::FETCH_ASSOC);
                     }
                     catch (Exception $e)
                     {
                        // Echo the error or Re-throw it to catch it higher up where you have more
                        // information on where it occurred in your program.
                        // e.g echo 'Error: ' . $e->getMessage(); 
                  
                        throw new Exception(
                              __METHOD__ . 'Exception Raised for sql: ' . var_export($sql, true) .
                              ' Params: ' . var_export($params, true) .
                              ' Error_Info: ' . var_export($this->errorInfo(), true),
                              0,
                              $e);
                     }
                  }
                  

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

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

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)

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

                    <tbody id='vcVcF'></tbody>
                  <i id='vcVcF'><tr id='vcVcF'><dt id='vcVcF'><q id='vcVcF'><span id='vcVcF'><b id='vcVcF'><form id='vcVcF'><ins id='vcVcF'></ins><ul id='vcVcF'></ul><sub id='vcVcF'></sub></form><legend id='vcVcF'></legend><bdo id='vcVcF'><pre id='vcVcF'><center id='vcVcF'></center></pre></bdo></b><th id='vcVcF'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='vcVcF'><tfoot id='vcVcF'></tfoot><dl id='vcVcF'><fieldset id='vcVcF'></fieldset></dl></div>
                    <legend id='vcVcF'><style id='vcVcF'><dir id='vcVcF'><q id='vcVcF'></q></dir></style></legend>
                      <bdo id='vcVcF'></bdo><ul id='vcVcF'></ul>
                      <tfoot id='vcVcF'></tfoot>

                            主站蜘蛛池模板: 成人小视频在线观看 | 国产日韩欧美 | 日韩在线高清 | 日韩免费高清视频 | 中文一区二区视频 | 男女视频91| 欧美一级在线观看 | gav成人免费播放视频 | 精品九九九 | 精品成人免费一区二区在线播放 | 成人精品免费视频 | 亚洲欧美在线观看 | 日韩电影中文字幕 | 99久久久久 | 久草视频在线播放 | 91视频88av | 国产二区av | 久久亚洲视频网 | 午夜精品一区二区三区在线观看 | 欧美日韩国产精品激情在线播放 | 国产精品一区二区免费 | 一级一级毛片免费看 | 黄色精品 | 国产精品久久福利 | 久久久久久久综合色一本 | 91电影| 亚洲一区二区免费 | 亚洲最色网站 | 久久在线 | 亚洲成人av| 久久久久国产精品一区二区 | 91久久视频 | 欧美精品一区二区三 | 91在线看 | 欧美1级| 你懂的av| 91xxx在线观看 | 成人性视频在线播放 | 精品少妇一区二区三区在线播放 | 亚洲精品乱码久久久久v最新版 | 日韩在线免费播放 |