Last active
December 18, 2015 11:48
-
-
Save fhferreira/5777832 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 myDBH { | |
| private $db_info = array("host" => "localhost", "dbname" => "myDB", "username" => "myDB_user", "password" => "myDB_password"); | |
| private $dbh; | |
| public static $instance = NULL; | |
| public function __construct(array $db_info = null) { | |
| if(isset($db_info)) { | |
| foreach($db_info as $key_name => $key_value) { | |
| if(!in_array($key_name, array("host", "db_name", "username", "password") || empty($key_value))) { | |
| throw new Exception("Invalid key passed!"); | |
| } | |
| $this->db_info = $db_info; | |
| } | |
| } | |
| $this->dbh = new PDO("mysql:host={$this->db_info['host']};dbname={$this->db_info['db_name']}", $this->db_info['username'], $this->db_info['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); | |
| $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
| $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); | |
| $this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | |
| } | |
| public static function getInstance() { | |
| if(!isset(self::$instance)) { | |
| self::$instance = new myDBH(); | |
| } | |
| return self::$instance; | |
| } | |
| public function fetch_rows($sql) { | |
| $rst = $this->dbh->query($sql); | |
| return $rst->fetchAll(); | |
| } | |
| public function fetch_row($sql) { | |
| $rst = $this->dbh->query($sql); | |
| return $rst->fetch(); | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment