Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PDO_ext extends PDO {
- // $statements: stores an array of prepared statements that have been used in order to reuse old ones
- // Syntax: array(string(statement), object(mysqli_stmt), int(count))
- private $statements = array();
- // $debug_mode will gather additional information about all queries performed
- // This also allows you to display total queries and total SQL execution time
- public $debug_mode = true;
- // $queries: stores an array of all queries and their execution times
- // Requires $debug_mode to be on
- // Syntax: array(string(statement), double(execution time))
- private $queries = array();
- public function query($query, $stmt_array = false) {
- // Get start time
- if ($this->debug_mode == true) {
- $this->queries[] = array($query,null);
- $start = $this->getMicrotimeFloat();
- }
- // Cache old prepared statements so that we don't need a new object every time
- // This saves memory if executing the same statement over and over
- $used_already = $this->get_used_statement($query);
- if ($used_already !== false) {
- $stmt = $used_already;
- }
- // Object doesn't exist so create a new one
- else {
- // Prepare the statement
- try {
- $stmt = $this->prepare($query);
- }
- catch(PDOException $e) {
- $bt = debug_backtrace();
- echo 'Unable to prepare statement called from file <b>'.$bt[0]['file'].'</b> on line <b>'.$bt[0]['line'].'</b>:';
- echo '<br /><br />Mysql error: <i>'.$e->errorInfo[2].'</i>';
- exit();
- }
- $this->statements[] = array($query, $stmt, 1);
- $stmt->setFetchMode(PDO::FETCH_ASSOC);
- }
- // Bind the parameters if there are any
- if (is_array($stmt_array)) {
- foreach ($stmt_array as $vars) {
- if (isset($vars[2])) {
- switch($vars[2]) {
- case 'i':
- $type = PDO::PARAM_INT;
- break;
- case 's':
- $type = PDO::PARAM_STR;
- break;
- case 'b':
- $type = PDO::PARAM_BOOL;
- break;
- default:
- $type = $vars[2];
- }
- }
- else $type = false;
- $stmt->bindParam($vars[0],$vars[1],$type);
- }
- }
- // Execute the query
- try {
- set_error_handler(array($this, 'errorHandler')); // Set our own error handler to exit on warnings
- $stmt->execute(); // Execute the query
- restore_error_handler(); // Restore error handler on success
- }
- catch(PDOException $e) {
- $bt = debug_backtrace();
- echo 'Unable to execute query called from file <b>'.$bt[0]['file'].'</b> on line <b>'.$bt[0]['line'].'</b>';
- echo '<br /><br />Mysql error: <i>';
- echo $e->errorInfo[2];
- echo '</i><br /><br />Debug info: <pre>';
- $stmt->debugDumpParams();
- // echo '<br /><br />Additional debug info:<br />'; print_r($bt);
- echo '</pre>';
- exit();
- }
- // Get end time
- if ($this->debug_mode == true) {
- $end = $this->getMicrotimeFloat();
- $this->queries[ count($this->queries)-1 ][ 1 ] = ($end - $start);
- }
- // Get number of columns in the result set. If it is a SELECT, this will be greater than 0
- $colcount = $stmt->columnCount();
- if ($colcount > 0) {
- $result = $stmt->fetchAll();
- }
- // It is a data manipulation query, so $colcount will be 0.
- else $result = $stmt->rowCount();
- return $result;
- }
- // Finds out if that statement has been used already to recycle it.
- // This saves memory usage. For large numbers of queries,you can easily run out.
- private function get_used_statement($query) {
- foreach ($this->statements as $stmt) {
- if ($query == $stmt[0]) {
- $stmt[2]++;
- return $stmt[1];
- }
- }
- return false;
- }
- private function errorHandler($errno, $errstr, $errfile, $errline) {
- // This prints a 'warning' or a 'notice' error so that the script can exit
- echo $errstr;
- }
- // Used for getting the total execution time of the queries
- private function getMicrotimeFloat () {
- list ($msec, $sec) = explode(' ', microtime());
- $microtime = (float)$msec + (float)$sec;
- return $microtime;
- }
- // Prints a history of the queries used along with their execution time
- public function printDebugInfo() {
- if ($this->debug_mode == false) {
- echo '<br /><b>Warning</b>: Unable to call printDebugInfo() since $debug_mode is false.';
- return false;
- }
- $totals = $this->getQueryTotals();
- if ($totals[0] == 0) {
- echo '<br /><b>Notice</b>: printDebugInfo() was called, however no queries have been executed yet.';
- return false;
- }
- echo "<h3>Query history:</h3><ul>";
- if ($totals[0] > 1) {
- echo '<p>'.$totals[0].' queries. Total execution time for all queries: '.sprintf('%F12',$totals[1]).' sec.';
- }
- foreach ($this->queries as $query_arr) {
- echo '<li>'.$query_arr[0].' (execution time: '.sprintf('%F12',$query_arr[1]).' sec)</li>';
- }
- echo "</ul>";
- }
- public function getQueryTotals() {
- $num_queries = count($this->queries);
- $total_time = 0;
- foreach ($this->queries as $query_arr) {
- $total_time += $query_arr[1];
- }
- return array($num_queries,$total_time);
- }
- }
- // Connect to database
- try {
- switch ($driver) {
- case 'postgre':
- $pdo = new PDO_ext("pgsql:host=$host;dbname=$dbname;user=$user;password=$pass");
- break;
- case 'sqlite':
- $pdo = new PDO_ext($sqlite_db);
- break;
- case 'mssql':
- $pdo = new PDO_ext("mssql:host=$host;dbname=$dbname, $user, $pass");
- break;
- case 'sybase':
- $pdo = new PDO_ext("sybase:host=$host;dbname=$dbname, $user, $pass");
- break;
- default:
- $pdo = new PDO_ext("mysql:host=$host;dbname=$dbname", $user, $pass);
- }
- $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
- }
- catch(PDOException $e) {
- die ("DB connection error ($driver): ".$e->getMessage());
- }
- // Example usage:
- $query = 'SELECT `username`,`id`,`email` FROM `users` WHERE username = :username AND `password` != :password';
- $vars = array(array(':username','myuser','s'),array(':password','12345','i'));
- $result = $pdo->query($query, $vars);
- $result2 = $pdo->query("SELECT * FROM users",'asdf');
- $pdo->printDebugInfo();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement