Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Db extends \PDO
- {
- private $_host;
- private $_dbname;
- private $_user;
- private $_pass;
- public function __construct($config)
- {
- if(empty($config)) {
- throw new Exception('Cannot connect to database. Config array seems to be empty.');
- }
- $this->_host = $config['host'];
- $this->_dbname = $config['dbname'];
- $this->_user = $config['user'];
- $this->_pass = $config['pass'];
- $format = 'mysql:host=%s;dbname=%s;charset=utf8';
- $dsn = sprintf($format, $this->_host, $this->_dbname);
- try {
- parent::__construct($dsn, $this->_user, $this->_pass, \NULL);
- parent::setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $e) {
- throw new Exception('Cannot connect to database. Please check your DSN string. '.$e->getMessage(), $e->getCode());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement