Advertisement
Guest User

Untitled

a guest
Jul 28th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.08 KB | None | 0 0
  1. ```php
  2. <?php
  3.  
  4. require_once dirname(__DIR__) . '/inc/Config.php';
  5.  
  6. class Database
  7. {
  8.  
  9.   private static $_instance;
  10.   private static $_db_name;
  11.   private static $_db_user;
  12.   private static $_db_pass;
  13.   private static $_connection;
  14.  
  15.   function __destruct()
  16.   {
  17.     $this->_connection = null;
  18.   }
  19.  
  20.   public static function getInstance()
  21.   {
  22.     if (static::$_instance == null) {
  23.       static::$_instance = new static();
  24.     }
  25.  
  26.     if (static::$_connection == null) {
  27.       try {
  28.         static::$_db_name = Config::$db_name;
  29.         static::$_db_user = Config::$db_user;
  30.         static::$_db_pass = Config::$db_pass;
  31.         static::$_connection = new PDO('mysql:host=sql9.freesqldatabase.com;dbname=' . static::$_db_name, static::$_db_user, static::$_db_pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  32.         static::$_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  33.       } catch (PDOException $ex) {
  34.         die('FATAL ERROR: ' . $ex->getMessage());
  35.       }
  36.     }
  37.  
  38.     return static::$_instance;
  39.   }
  40.  
  41.   public static function getConnection()
  42.   {
  43.     if (static::$_instance == null) {
  44.       static::getInstance();
  45.     }
  46.  
  47.     return static::$_connection;
  48.   }
  49.  
  50.   public static function tableExists($table)
  51.   {
  52.  
  53.     if (static::$_instance == null) {
  54.       static::$_instance = new static();
  55.     }
  56.  
  57.     if (static::$_connection == null) {
  58.       try {
  59.         static::$_connection = new PDO('mysql:host=sql9.freesqldatabase.com;dbname=' . static::$_db_name . ';charset=utf8', static::$_db_user, static::$_db_pass);
  60.         static::$_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  61.       } catch (PDOException $ex) {
  62.         die('FATAL ERROR: ' . $ex->getMessage());
  63.       }
  64.     }
  65.  
  66.     try {
  67.       $result = static::$_connection->query("SELECT 1 FROM $table LIMIT 1");
  68.     } catch (Exception $e) {
  69.       return false;
  70.     }
  71.  
  72.     return $result !== false;
  73.   }
  74.  
  75.   protected function __construct()
  76.   {
  77.   }
  78.  
  79.   private function __clone()
  80.   {
  81.   }
  82.  
  83.   private function __wakeup()
  84.   {
  85.   }
  86.  
  87. }
  88.  
  89. ?>
  90. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement