PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
/**
* Extends PDO and logs all queries that are executed and how long
* they take, including queries issued via prepared statements
*/
class ExtendedPDO extends PDO {
/**
* constructor
* @param string $dsn
* @param string $username
* @param string $password
*/
public function __construct($dsn, $username, $password) {
parent::__construct($dsn, $username, $password);
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('ExtendedPDOStatement', array()));
}
// write your methods here, eg.
}
class ExtendedPDOStatement extends ExtendedPDO {
// write your methods here
}
<b>Warning</b>: PDO::setAttribute() [<a href='pdo.setattribute'>pdo.setattribute</a>]: SQLSTATE[HY000]: General error: user-supplied statement class must be derived from PDOStatement in <b>C:\Program Files\Zend\Apache2\htdocs\gcms\gcms\applications\backend\framework.php</b> on line <b>214</b><br />
Last edited by spamyboy on Sat May 30, 2009 4:46 am, edited 1 time in total.
/**
* Extends PDO and logs all queries that are executed and how long
* they take, including queries issued via prepared statements
*/
class ExtendedPDO extends PDO
{
# logged queries with their execution time
public static $log = array();
/**
* constructor
*
* @param string $dsn
* @param string $username
* @param string $password
* @param array $driver_options
* @return void
*/
public function __construct($dsn, $username, $password, $driver_options = array())
{
parent::__construct($dsn, $username, $password, $driver_options);
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('ExtendedPDOStatement', array($this)));
}
/**
* Print out log when all references to a PDO object are remowed.
*
* @return void
*/
public function __destruct()
{
self::printLog();
}
/**
* When query is called, record the time it takes and then log the query.
*
* @param string $query
* @return PDOStatement object
*/
public function query($query)
{
$start = microtime(true);
$result = parent::query($query);
$time = microtime(true) - $start;
LoggedPDO::$log[] = array(
'query' => $query,
'time' => round($time * 1000, 3)
);
die(var_dump($result));
return $result;
}
/**
* Print out gathered data about queries
*
* @return void
*/
private static function printLog()
{
print_r(self::$log);
}
}
class ExtendedPDOStatement extends PDOStatement
{
public $dbh;
// Actualy, shouldn't this be a (PDOStatement $dbh) object?
protected function __construct(ExtendedPDO $dbh) {
$this->dbh = $dbh;
}
/**
* When execute is called, record the time it takes and then log the query.
*/
public function execute($parameters = array())
{
return $this->dbh->execute($parameters);
}
}
Fatal error: Call to undefined method ExtendedPDO::execute() in C:\Program Files\Zend\Apache2\htdocs\gcms\gcms\applications\backend\framework.php on line 278
Fatal error: Call to undefined method ExtendedPDO::execute() in C:\Program Files\Zend\Apache2\htdocs\gcms\gcms\applications\backend\framework.php on line 278