Guest User

Untitled

a guest
Aug 12th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. <?php
  2.  
  3. class Database extends PDO {
  4.  
  5. private $host = '127.0.0.1';
  6. private $db = 'database_name';
  7. private $user = 'anything_but_root';
  8. private $pass = 'anything_but_password';
  9. private $charset = 'utf8mb4';
  10.  
  11. public function __construct() {
  12. $dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
  13. $opt = [
  14. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  15. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
  16. PDO::ATTR_EMULATE_PREPARES => false
  17. ];
  18.  
  19. try {
  20. $this->$db = new PDO($dsn, $this->user, $this->pass, $opt);
  21. }
  22. catch(PDOException $e) {
  23. $this->error = $e->getMessage(); # For development ONLY!
  24. /* return FALSE; */ # uncomment in production.
  25. }
  26. }
  27.  
  28. public function close_conn() {
  29. $this->pdo = null; /* Set the PDO object to null to close the connection */
  30. }
  31. }
Add Comment
Please, Sign In to add comment