public function __construct() { global $dbc; //read from config.php $c = new CONNECT(); $this->conn = $c->getConnection(); $this->table_posts = $dbc['table'] . "posts"; $this->table_config = $dbc['table'] . "config"; }
/** * * Return DB instance or create intitial connection * * @return object (PDO) * * @access public * */ public static function getInstance() { if (!self::$instance) { self::$instance = (include_once "{$db_type}.php"); //new PDO(driver, db_settings); self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } return self::$instance; }
<?php require_once 'config.php'; class CONNECT { public $conn; public function __construct() { global $dbc; $this->conn = mysqli_connect($dbc['servername'], $dbc['username'], $dbc['password'], $dbc['dbname']); } public function getConnection() { return $this->conn; } } if (isset($dbc)) { // Create connection $db = new CONNECT(); // Check connection if (!$db->getConnection()) { die("Connection failed: " . mysqli_connect_error()); } }