/** * Static method get * * @param array $group * @return \helpers\database */ public static function get($group = false) { // Determining if exists or it's not empty, then use default group defined in config $group = !$group ? array('type' => DB_TYPE, 'host' => DB_HOST, 'name' => DB_NAME, 'user' => DB_USER, 'pass' => DB_PASS) : $group; // Group information $type = $group['type']; $host = $group['host']; $name = $group['name']; $user = $group['user']; $pass = $group['pass']; // ID for database based on the group information $id = "{$type}.{$host}.{$name}.{$user}.{$pass}"; // Checking if the same if (isset(self::$instances[$id])) { return self::$instances[$id]; } try { // I've run into problem where // SET NAMES "UTF8" not working on some hostings. // Specifiying charset in DSN fixes the charset problem perfectly! //$instance = new Database("$type:host=$host;dbname=$name;charset=utf8", $user, $pass); $instance = pg_connect("host={$host} port=5432 dbname={$name} user={$user} password={$pass}"); $instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Setting Database into $instances to avoid duplication self::$instances[$id] = $instance; return $instance; } catch (PDOException $e) { //in the event of an error record the error to ErrorLog.html Logger::newMessage($e); Logger::customErrorMsg(); } }
/** * create database connection */ function __construct() { try { parent::__construct(DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS); $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); } catch (PDOException $e) { //in the event of an error record the error to errorlog.html Logger::newMessage($e); logger::customErrorMsg(); } }