Last active
December 18, 2015 12:58
-
-
Save Erackron/5786072 to your computer and use it in GitHub Desktop.
This is a simple MySQL database handler in PHP using PDO.
The default database type used is MySQL, but this can be changed by making the following changes:
1: Update the default port number on line 28.
2: Update the DSN on line 30 to reflect the database system used.
3: Change the SQL query on line 44 to the equivalent of the SQL query to select…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Jorai Rijsdijk | |
* @desc A simple MySQL database handler in PHP using PDO. | |
* @version 1.3 | |
* Note: | |
* The default database type used is MySQL, but this can be changed by making the following changes: | |
* 1: Update the default port number on line 28 (constructor). | |
* 2: Update the DSN on line 30 (new PDO object) to reflect the database system used. | |
* 3: Change the SQL query on line 44 (in the setDB function) to the equivalent of the SQL query to select a different database. | |
*/ | |
Class dbHandler{ | |
const INTERNAL_HANDLING = 1; | |
const THROW_EXCEPTION = 2; | |
const IGNORE_ERRORS = 3; | |
private $pdo = null, $stmt = null; | |
private $errorHandling = self::INTERNAL_HANDLING; | |
/** | |
* Create a new dbHandler object to interact with a database using PDO. All parameters are optional. | |
* @param string $database The database to connect to | |
* @param string $user The user to login with | |
* @param string $password The password belonging to the user | |
* @param string $server The hostname of the server to connect to | |
* @param int $port The server port for the database | |
*/ | |
function __construct($database, $user, $password, $server = "localhost", $port = 3306){ | |
try{ | |
$this->pdo = new PDO('mysql:host='.$server.';port='.$port.';dbname='.$database.';charset=utf8',$user,$password); | |
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); | |
} catch (PDOException $e){ | |
$this->processException($e); | |
} | |
} | |
/** | |
* Change the database to direct your queries to | |
* @param string $dbName The name of the database to connect to. | |
* @return boolean Whether or not the change succeeded. Error handling will be applied according to the settings. | |
*/ | |
public function setDB($dbName){ | |
return ($this->query("USE ?", array($dbName)))?true:false; | |
} | |
/** | |
* Process a query and get the PDOStatement object back | |
* @param string $query The query to process. Use :paramname for named parameters and ? for anonymous parameters. | |
* @param array $params The parameters array, just like you would use with PDO | |
* @return PDOStatement|boolean The PDOStatement object retreived from the database or false if something failed. | |
*/ | |
public function query($query, array $params = array()){ | |
try{ | |
if($this->pdo !== null){ | |
if(($this->stmt = $this->pdo->prepare($query))!==false){ | |
if($this->stmt->execute($params)) | |
return $this->stmt; | |
} | |
} | |
return false; | |
} catch (PDOException $e){ | |
$this->processException($e); | |
} | |
} | |
/** | |
* Get the latest insert id for an auto_increment column | |
* @return int The Latest insert id of an auto_increment column | |
*/ | |
public function lastInsertId($name = NULL){ | |
return $this->pdo->lastInsertId($name); | |
} | |
/** | |
* Set the errorHandling method to either INTERNAL_HANDLING, THROW_EXCEPTION or IGNORE_ERRORS | |
* @param int $handlingMethod The handling method preferred | |
*/ | |
public function setErrorHandling($handlingMethod){ | |
switch($handlingMethod){ | |
case self::INTERNAL_HANDLING: | |
case self::THROW_EXCEPTION: | |
case self::IGNORE_ERRORS: | |
$this->errorHandling = $handlingMethod; | |
default: | |
} | |
} | |
/** | |
* This private method processes exceptions based on the errorHandling settings | |
* @param PDOException $e The exception to process | |
* @throws PDOException Only if errorHandling is set to dbHandler::THROW_EXCEPTION | |
*/ | |
private function processException(PDOException &$e){ | |
switch($this->errorHandling){ | |
case self::INTERNAL_HANDLING: | |
self::handleException($e); | |
break; | |
case self::THROW_EXCEPTION: | |
throw $e; | |
break; | |
default: | |
} | |
} | |
/** | |
* Handle a PDOException by showing the stacktrace properly and dieing. | |
* @param PDOException $e The exception to handle | |
*/ | |
public static function handleException(PDOException &$e){ | |
$trace = $e->getTrace(); | |
$msg = "<span class='error'><b>An error has occured: </b><br><pre>"; | |
$msg .= $e->getMessage() . '<br>'; | |
foreach($trace as $line){ | |
$msg .= "\t<b>at</b> "; | |
if(!empty($line['class'])){ | |
$msg .= $line['class']; | |
$msg .= $line['type']; | |
} | |
$msg .= $line['function'] . '()'; | |
$msg .= " <b>in</b> " . self::trimFilePath($line['file']) . " (<b>line</b> " . $line['line'] . ")<br>"; | |
} | |
$msg .= '</pre></span>'; | |
die($msg); | |
} | |
/** | |
* Trim the file path to start at the Document_Root | |
* @param string $path The path to trim | |
* @return string The trimmed path | |
*/ | |
private static function trimFilePath($path){ | |
return str_replace($_SERVER['DOCUMENT_ROOT'], "", $path, $count); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment