Skip to content

Instantly share code, notes, and snippets.

@scottmac
Created November 4, 2010 19:56
Show Gist options
  • Save scottmac/663080 to your computer and use it in GitHub Desktop.
Save scottmac/663080 to your computer and use it in GitHub Desktop.
<?php
class DatabaseStatementBase extends PDOStatement {
public $dbh;
protected function __construct($dbh) {
$this->dbh = $dbh;
$this->setFetchMode(PDO::FETCH_OBJ);
}
}
class DatabaseConnection extends PDO {
function __construct($dsn, $username, $password, $driver_options = array())
{
parent::__construct($dsn, $username, $password, $driver_options);
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS,
array('DatabaseStatementBase', array($this)));
}
}
$pdo = new DatabaseConnection('sqlite::memory:', '', '');
$pdo->exec('CREATE TABLE foo (bar STRING)');
$pdo->exec("INSERT INTO foo VALUES ('ABC')");
$result = $pdo->query("SELECT * FROM foo");
foreach ($result as $row) {
var_dump($row);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment