Created
February 12, 2014 16:35
-
-
Save Palmr/8959221 to your computer and use it in GitHub Desktop.
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 | |
class Database { | |
private $dbHandle; | |
private $preparedStatements = array(); | |
public $rowCount; | |
public $lastInsertId; | |
public function __construct($host, $database, $username, $password, $errorMode='silent') { | |
try { | |
$this->dbHandle = new PDO('mysql:host=' . $host . ';dbname=' . $database, $username, $password); | |
if ($errorMode == 'exception') { | |
$this->dbHandle->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); | |
} | |
else if ($errorMode == 'warning') { | |
$this->dbHandle->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); | |
} | |
else { | |
$this->dbHandle->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT ); | |
} | |
} | |
catch(PDOException $e) { | |
die($e->getMessage()); | |
} | |
return $this->dbHandle; | |
} | |
public function prepare($name, $query) { | |
//debug warning if overriding statement | |
try { | |
$this->preparedStatements[$name] = $this->dbHandle->prepare($query); | |
} | |
catch(PDOException $e) { | |
die($e->getMessage()); | |
} | |
} | |
public function execute($statement, $dataArray=null, $dataArrayType='STRINGS') { | |
if (!array_key_exists($statement, $this->preparedStatements)) { | |
die('Cannot find statement: ' . htmlentities($statement)); | |
} | |
try { | |
$stmt = $this->preparedStatements[$statement]; | |
if ($dataArrayType != 'STRINGS') { | |
foreach ($dataArray as $bindKey => $bindItem) { | |
$stmt->bindParam($bindKey, $bindItem[0], $bindItem[1]); | |
} | |
$stmt->execute(); | |
} | |
else { | |
$stmt->execute($dataArray); | |
} | |
$this->lastInsertId = $this->dbHandle->lastInsertId(); | |
$this->rowCount = $stmt->rowCount(); | |
} | |
catch(PDOException $e) { | |
die($e->getMessage()); | |
} | |
return $stmt; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment