2011-03-03 125 views
9

下面是我到目前爲止出來的數據庫連接類,但我會通過擴展PDO類本身進行改進,擴展PDO類

<?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; 
    } 
} 

這是我如何實例化類,

# 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類,

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

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

我已經做了一些研究,在網上,我發現擴展PDO的這個基本結構,但我不明白它...

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

什麼是$ds N個變量的呢?如何將我的$hostname變量傳遞給擴展的pdo類?

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

從mysqli移動到pdo是非常困難的!

謝謝。

+2

看看你寫的是什麼,你真的不需要擴展PDO類。你只會讓它比需要的更難。 – 2011-03-03 01:03:44

+0

延長pdo是一件壞事?我以前有一個獨立的mysqli類,但我被告知擴展mysqli更好,所以我認爲它必須與pdo相同? – laukok 2011-03-03 01:10:34

+1

你不需要重寫構造函數btw。 – 2011-06-06 04:54:34

回答

3

$ dsn是數據源名稱。它爲你處理你的主機名。您可以使用這樣的:

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

隨着$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);您已設置例外線的時候發生錯誤(我喜歡)待提高,所以在您的擴展類,您可以處理在異常處理程序錯誤。如果您在擴展PDO類有一個名爲getAssoc方法則是這樣的:

/// 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); 
    } 
} 
+0

謝謝保羅。你能給我一個示例代碼,你可以處理異常處理程序中的錯誤...? – laukok 2011-03-03 01:58:16

+0

感謝您的編輯,保羅! – laukok 2011-03-03 02:28:27

4

我會集中在哪些類需要做的,而不是試圖重新寫PDO。我有同樣的想法,因爲我雖然會簡化代碼,但它不會。你最終花更多的時間研究如何間接與PDO接口。