Created
May 2, 2014 09:55
-
-
Save Gmanweb/16831b195956bb296d28 to your computer and use it in GitHub Desktop.
PHP DATABASE CONNECTION CLASS, Modified to suit me. Inspired by Reference http://www.whatsnoodle.com/creating-a-singleton-class-in-php AND https://gist.github.com/jonashansen229/4534794
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 | |
| $connection = DatabaseConnection::getInstance(); | |
| $query = "SELECT * FROM fixturesresults"; | |
| $result = mysqli_query($connection->getConnection() , $query); | |
| while($row = mysqli_fetch_array($result)){ | |
| // what data you need here Gman :) | |
| } | |
| ?> |
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 | |
| /* | |
| * Mysql database class - only one connection allowed | |
| */ | |
| ///////////////////////////////////////////////////////////////////////////////////// | |
| ////// Reference http://www.whatsnoodle.com/creating-a-singleton-class-in-php /////// | |
| ///////////////// https://gist.github.com/jonashansen229/4534794 //////////////////// | |
| ///////////////////////////////////////////////////////////////////////////////////// | |
| class DatabaseConnection { | |
| private $_connection; | |
| private static $_instance; //The single instance | |
| private $_host = "localhost"; | |
| private $_username = "root"; | |
| private $_password = ""; | |
| private $_database = "worldcup2014db"; // database name here | |
| //private static $connection = null; | |
| public static function getInstance(){ | |
| if(!self::$_instance) { // If no instance then make one | |
| self::$_instance = new self(); | |
| } | |
| return self::$_instance; | |
| } | |
| /** | |
| * Protected constructor to prevent creating a new instance of the | |
| * *Singleton* via the `new` operator from outside of this class. | |
| */ | |
| // Constructor | |
| private function __construct() { | |
| $this->_connection = new mysqli($this->_host, $this->_username, | |
| $this->_password, $this->_database); | |
| // Error handling | |
| if(mysqli_connect_error()) { | |
| trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(), | |
| E_USER_ERROR); | |
| } | |
| } | |
| // Magic method clone is empty to prevent duplication of connection | |
| private function __clone() { } | |
| // Get mysqli connection | |
| public function getConnection() { | |
| return $this->_connection; | |
| } | |
| protected function _close() { | |
| if ($this->_connection != NULL) { | |
| $this->_connection = NULL; | |
| } | |
| } | |
| public function __destruct() { | |
| $this->_close(); | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment