Skip to content

Instantly share code, notes, and snippets.

@ShawnAbshire
Created December 13, 2014 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShawnAbshire/674f67604bec706c930b to your computer and use it in GitHub Desktop.
Save ShawnAbshire/674f67604bec706c930b to your computer and use it in GitHub Desktop.
SafePDO
<?php
use PDO;
Class SafePDO extends PDO {
private $server = "";
private $username = "";
private $pass = "";
private $database = "";
public static function exception_handler($exception) {
die('Uncaught exception: ' . $exception->getMessage());
}
public function __construct() {
// Temporarily change the PHP exception handler while we . . .
set_exception_handler(array(__CLASS__, 'exception_handler'));
// . . . create a PDO object
$dsn = 'mysql:host=' . $this->server . ';dbname=' . $this->database;
parent::__construct($dsn, $this->username, $this->pass);
// Change the exception handler back to whatever it was before
restore_exception_handler();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment