Created
April 21, 2014 03:42
-
-
Save joshblack/11131617 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 DB { | |
/** | |
* The PDO object | |
* | |
* @var object | |
*/ | |
private $pdo; | |
/** | |
* See if connected to Database | |
* | |
* @var boolean | |
*/ | |
private $isConnected; | |
/** | |
* Connection settings for the Database | |
* | |
* @var array | |
*/ | |
private $settings; | |
/** | |
* Default Database constructor | |
* | |
* @param none | |
* @return none | |
*/ | |
public function __construct() { | |
$this->connect(); | |
} | |
/** | |
* Connect to the database | |
* | |
* @param none | |
* @return none | |
*/ | |
public function connect() { | |
// Grab our settings | |
$this->settings = parse_ini_file('settings.ini.php'); | |
$dsn = 'mysql:dbname='.$this->settings["dbname"].';host='.$this->settings["host"].''; | |
try | |
{ | |
// Read settings from INI file and create our PDO | |
$this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); | |
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); | |
// Connection succeeded | |
$this->isConnected = true; | |
} | |
catch (PDOException $e) | |
{ | |
echo $this->exceptionMessage($e->getMessage()); | |
die(); | |
} | |
} | |
/** | |
* Close the connection to the Database | |
* | |
* @param none | |
* @return none | |
*/ | |
public function closeConnection() { | |
$this->pdo = null; | |
} | |
/** | |
* Query the Database | |
* | |
* @param string, array | |
* @return array | |
*/ | |
public function query($query, $params = []) { | |
$stmt = $this->pdo->prepare($query); | |
$stmt->execute($params); | |
return $stmt ?: null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment