Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```php
- <?php
- require_once dirname(__DIR__) . '/inc/Config.php';
- class Database
- {
- private static $_instance;
- private static $_db_name;
- private static $_db_user;
- private static $_db_pass;
- private static $_connection;
- function __destruct()
- {
- $this->_connection = null;
- }
- public static function getInstance()
- {
- if (static::$_instance == null) {
- static::$_instance = new static();
- }
- if (static::$_connection == null) {
- try {
- static::$_db_name = Config::$db_name;
- static::$_db_user = Config::$db_user;
- static::$_db_pass = Config::$db_pass;
- 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"));
- static::$_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $ex) {
- die('FATAL ERROR: ' . $ex->getMessage());
- }
- }
- return static::$_instance;
- }
- public static function getConnection()
- {
- if (static::$_instance == null) {
- static::getInstance();
- }
- return static::$_connection;
- }
- public static function tableExists($table)
- {
- if (static::$_instance == null) {
- static::$_instance = new static();
- }
- if (static::$_connection == null) {
- try {
- static::$_connection = new PDO('mysql:host=sql9.freesqldatabase.com;dbname=' . static::$_db_name . ';charset=utf8', static::$_db_user, static::$_db_pass);
- static::$_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $ex) {
- die('FATAL ERROR: ' . $ex->getMessage());
- }
- }
- try {
- $result = static::$_connection->query("SELECT 1 FROM $table LIMIT 1");
- } catch (Exception $e) {
- return false;
- }
- return $result !== false;
- }
- protected function __construct()
- {
- }
- private function __clone()
- {
- }
- private function __wakeup()
- {
- }
- }
- ?>
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement