Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database extends PDO {
- private $host = '127.0.0.1';
- private $db = 'database_name';
- private $user = 'anything_but_root';
- private $pass = 'anything_but_password';
- private $charset = 'utf8mb4';
- public function __construct() {
- $dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
- $opt = [
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
- PDO::ATTR_EMULATE_PREPARES => false
- ];
- try {
- $this->$db = new PDO($dsn, $this->user, $this->pass, $opt);
- }
- catch(PDOException $e) {
- $this->error = $e->getMessage(); # For development ONLY!
- /* return FALSE; */ # uncomment in production.
- }
- }
- public function close_conn() {
- $this->pdo = null; /* Set the PDO object to null to close the connection */
- }
- }
Add Comment
Please, Sign In to add comment