title: extending PDO class
body: <p>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,</p>
  
  <pre><code>&lt;?php
  class database
  {
      protected $connection = null;
  
      #make a connection
      public function __construct($hostname,$dbname,$username,$password)
      {
          try 
          {
              # MySQL with PDO_MYSQL  
              $this-&gt;connection = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
              $this-&gt;connection-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
          }
          catch (PDOException $e) 
          {
              $this-&gt;connection = null;
              die($e-&gt;getMessage());
          }
      }
  
      #get the number of rows in a result
      public function num_rows($query)
      {
          # create a prepared statement
          $stmt = $this-&gt;connection-&gt;prepare($query);
  
          if($stmt) 
          {
              # execute query 
              $stmt-&gt;execute();
  
              return $stmt-&gt;rowCount();
          } 
          else
          {
              return self::get_error();
          }
      }
  
      #display error
      public function get_error() 
      {
          $this-&gt;connection-&gt;errorInfo();
      }
  
      # closes the database connection when object is destroyed.
      public function __destruct()
      {
          $this-&gt;connection = null;
      }
  }
  ?&gt;
  </code></pre>
  
  <p>extended class,</p>
  
  <pre><code>class database extends PDO
  {
  
      #make a connection
      public function __construct($hostname,$dbname,$username,$password)
      {
          parent::__construct($hostname,$dbname,$username,$password);
  
          try 
          { 
              $this-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
          }
          catch (PDOException $e) 
          {
              die($e-&gt;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-&gt;execute();
  
              return $stmt-&gt;rowCount();
          } 
          else
          {
              return self::get_error();
          }
      }
  
      #display error
      public function get_error() 
      {
          $this-&gt;connection-&gt;errorInfo();
      }
  
      # closes the database connection when object is destroyed.
      public function __destruct()
      {
          $this-&gt;connection = null;
      }
  }
  </code></pre>
  
  <p>This is how I instantiate the class,</p>
  
  <pre><code># 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-&gt;num_rows($sql);
  </code></pre>
  
  <p>But I have errors when I call this extended pdo class,</p>
  
  <blockquote>
    <p>Warning: PDO::__construct() expects
    parameter 4 to be array, string given
    in C:\wamp\www\xx\class_database.php
    on line xx</p>
    
    <p>Fatal error: Call to a member function
    setAttribute() on a non-object in
    C:\wamp\www\xx\class_database.php on
    line xx</p>
  </blockquote>
  
  <p>I have done some research online, I found this basic structure of extending pdo but I dont understand it...</p>
  
  <pre><code>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);
     }
  }
  </code></pre>
  
  <p>What is <code>$ds</code>n variable for? How can I pass my <code>$hostname</code> variable into extended pdo class?</p>
  
  <p>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? </p>
  
  <p>It is so difficult to move from mysqli to pdo!</p>
  
  <p>Thanks.</p>