-
-
Save TheSkallywag/0abe8ed1b6fa88a1d2a386fe9944700f 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 | |
| { | |
| private $_connection; | |
| private static $_instance; //The single instance | |
| private $_host = DB_HOST; | |
| private $_username = DB_USER; | |
| private $_password = DB_PASSWORD; | |
| private $_database = DB_DB; | |
| /* | |
| Get an instance of the Database | |
| @return Instance | |
| */ | |
| public static function getInstance() | |
| { | |
| if (!self::$_instance) { // If no instance then make one | |
| self::$_instance = new self(); | |
| } | |
| return self::$_instance; | |
| } | |
| // Constructor | |
| private function __construct() | |
| { | |
| try { | |
| $this->_connection = new \PDO("mysql:host=$this->_host;dbname=$this->_database", $this->_username, $this->_password); | |
| /*** echo a message saying we have connected ***/ | |
| echo 'Connected to database'; | |
| } catch (PDOException $e) { | |
| echo $e->getMessage(); | |
| } | |
| } | |
| // Magic method clone is empty to prevent duplication of connection | |
| private function __clone() | |
| { | |
| } | |
| // Get mysql pdo connection | |
| public function getConnection() | |
| { | |
| return $this->_connection; | |
| } | |
| } | |
| /** | |
| * And you can use it as such in a class | |
| * */ | |
| class Post { | |
| public function __construct(){ | |
| $db = Db::getInstance(); | |
| $this->_dbh = $db->getConnection(); | |
| } | |
| public function getPosts() | |
| { | |
| try { | |
| /*** The SQL SELECT statement ***/ | |
| $sql = "SELECT * FROM posts"; | |
| foreach ($this->_dbh->query($sql) as $row) { | |
| var_dump($row); | |
| } | |
| /*** close the database connection ***/ | |
| $this->_dbh = null; | |
| } catch (PDOException $e) { | |
| echo $e->getMessage(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment