Advertisement
GrottoFalls

database_wrapper.php

Dec 5th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. <?php
  2. define('DB_HOST', 'xxx');
  3. define('DB_NAME', 'xxx');
  4. define('DB_USER', 'xxx');
  5. define('DB_PASS', 'xxx');
  6. define('DB_CHAR', 'utf8');
  7.  
  8. class DB
  9. {
  10.     protected static $instance = null;
  11.  
  12.     protected function __construct() {}
  13.     protected function __clone() {}
  14.  
  15.     public static function instance()
  16.     {
  17.         if (self::$instance === null)
  18.         {
  19.             $opt  = array(
  20.                 PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
  21.                 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  22.                 PDO::ATTR_EMULATE_PREPARES   => false,
  23.                 PDO::ATTR_STRINGIFY_FETCHES => true,
  24.             );
  25.             $dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHAR;
  26.             self::$instance = new PDO($dsn, DB_USER, DB_PASS, $opt);
  27.         }
  28.         return self::$instance;
  29.     }
  30.  
  31.     public static function __callStatic($method, $args)
  32.     {
  33.         return call_user_func_array(array(self::instance(), $method), $args);
  34.     }
  35.  
  36.     public static function run($sql, $args = [])
  37.     {
  38.         if (!$args)
  39.         {
  40.              return self::instance()->query($sql);
  41.         }
  42.         $stmt = self::instance()->prepare($sql);
  43.         $stmt->execute($args);
  44.         return $stmt;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement