Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('DB_HOST', 'xxx');
- define('DB_NAME', 'xxx');
- define('DB_USER', 'xxx');
- define('DB_PASS', 'xxx');
- define('DB_CHAR', 'utf8');
- class DB
- {
- protected static $instance = null;
- protected function __construct() {}
- protected function __clone() {}
- public static function instance()
- {
- if (self::$instance === null)
- {
- $opt = array(
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- PDO::ATTR_EMULATE_PREPARES => false,
- PDO::ATTR_STRINGIFY_FETCHES => true,
- );
- $dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHAR;
- self::$instance = new PDO($dsn, DB_USER, DB_PASS, $opt);
- }
- return self::$instance;
- }
- public static function __callStatic($method, $args)
- {
- return call_user_func_array(array(self::instance(), $method), $args);
- }
- public static function run($sql, $args = [])
- {
- if (!$args)
- {
- return self::instance()->query($sql);
- }
- $stmt = self::instance()->prepare($sql);
- $stmt->execute($args);
- return $stmt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement