Skip to content

Instantly share code, notes, and snippets.

@TheBeachMaster
Forked from jonashansen229/class.database.php
Last active February 24, 2018 19:21
Show Gist options
  • Save TheBeachMaster/3ca29651998360f6e75d8fedb2e9eaad to your computer and use it in GitHub Desktop.
Save TheBeachMaster/3ca29651998360f6e75d8fedb2e9eaad to your computer and use it in GitHub Desktop.
PHP OOP Database class using MySQLi and Singleton.
<?php
class DbConnector {
private $_connection;
private static $_instance;
private $_host = "host";
private $_username = "username";
private $_password = "password";
private $_database = "dbname";
private $_dbport = 3306;
public static function getInstance() {
if(!self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
private function __construct() {
$this->_connection = new mysqli($this->_host, $this->_username,
$this->_password, $this->_database, $this->_dbport);
if(mysqli_connect_error()) {
trigger_error("Fatal MySQL Connection Error " . mysql_connect_error(),
E_USER_ERROR);
}
}
private function __clone() { }
public function getConnection() {
return $this->_connection;
}
}
class SomeOtherClass
{
$conn = DbConnector::getInstance();
$db = $conn->getConnection();
# use $db->*(*)......
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment