Advertisement
Guest User

Untitled

a guest
Jan 28th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. class Db extends \PDO
  2. {
  3. private $_host;
  4. private $_dbname;
  5. private $_user;
  6. private $_pass;
  7. public function __construct($config)
  8. {
  9. if(empty($config)) {
  10. throw new Exception('Cannot connect to database. Config array seems to be empty.');
  11. }
  12. $this->_host = $config['host'];
  13. $this->_dbname = $config['dbname'];
  14. $this->_user = $config['user'];
  15. $this->_pass = $config['pass'];
  16. $format = 'mysql:host=%s;dbname=%s;charset=utf8';
  17. $dsn = sprintf($format, $this->_host, $this->_dbname);
  18. try {
  19. parent::__construct($dsn, $this->_user, $this->_pass, \NULL);
  20. parent::setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  21. } catch (PDOException $e) {
  22. throw new Exception('Cannot connect to database. Please check your DSN string. '.$e->getMessage(), $e->getCode());
  23. }
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement