ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Bass class for PDO abstraction
Submitter: Jeff Griffiths (other recipes)Jeff Griffiths (other recipes)
Last Updated: 2007/06/25
Version no: 1.0
Category: Databases

 

Not Rated yet


Description:

This is a simple base class that extends PDO and provides some useful helper functions such as _getRows and _getAsRow()

Source: Text Source

class Foo extends PDO {
  /*
  * __constructor: create a new Foo object
  * @ param $cfg: usually a STDClass object with these properties
  * stdClass Object
  (
  [host] => localhost
  [db] => mysql
  [user] => username
  [pass] => password
  )
  */
  private $cfg;
  public $dsn;
  
  function __construct($cfg) {
    try {
      $this->dsn = "mysql:host=$cfg->host;dbname=$cfg->db";
      parent::__construct($this->dsn, $cfg->user, $cfg->pass);
      $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // throw exceptions
      $this->cfg = $cfg;
    }
    catch(Exception $e) {
      // XXX need to look for Array output on all ops
      print "Error: " . $e->getMessage();
    }
  }
  
  /* implement crazy code here */
  
  
  
  /* handy utility functions */
  
  function _getAsRow($sql, $i = 0) {
    try {
      $sth = $this->query($sql);
      $arr = $sth->fetchAll();
      $out = array();
      foreach($arr as $row) {
        $out[] = $row[$i];
      }
      return array(
        1,
        $out
      );
    }
    catch(Exception $e) {
      return $this->_err($e);
    }
  }
  // get all data returned by the query
  
  function _getRows($sql) {
    try {
      $sth = $this->query($sql);
      return array(
        1,
        $sth->fetchAll(PDO::FETCH_ASSOC)
      );
    }
    catch(Exception $e) {
      return $this->_err($e);
    }
  }
  /* run a query that doesn't return data, we want the affected rows instead */
  
  function boolQuery($sql) {
    try {
      $sth = $this->query($sql);
      return array(
        1,
        $sth->rowCount
      );
    }
    catch(Exception $e) {
      return $this->_err($e);
    }
  }
  /* return the expected error structure */
  private function _err($e) {
    return array(
      0,
      $e->getMessage()
    );
  }
  // dumper
  
  function _dump($data) { // pass in $_GET, etc
    $args = func_get_args();
    if (count($args) > 1) {
      return "\n<pre>\n" . print_r($args, 1) . "\n</pre>\n";
    } else {
      return "\n<pre>\n" . print_r($data, 1) . "\n</pre>\n";
    }
  }
}

Discussion:



Add comment

No comments.



Highest rated recipes:

1. DB_eSession PHP class ...

2. main - python-like if ...

3. iPHP: Semi-interactive ...

4. tinySendMail

5. Microsoft Access ...

6. Single Linked List

7. PHP MySQL Search Class

8. Pass Javascript arrays ...

9. Microsoft Access ...

10. Password generation




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.