/**
  * Инициализация подклчения к БД
  * @throws \Core\Database\Exception
  * @return boolean
  */
 public function init()
 {
     if (is_null(self::$db)) {
         try {
             // коннектимся к mysql
             self::$db = mysqli_connect(config('db_hostname'), config('db_username'), config('db_password'));
             mysqli_select_db(self::$db, config('db_database'));
             mysqli_query(self::$db, "SET NAMES " . config('db_char_set') . " COLLATE " . config('db_dbcollat'));
             var_dump("DB is loaded!");
         } catch (Exception $e) {
             throw new \Core\Database\Exception("Ошибка БД: " . mysqli_error(self::$db));
             return false;
         }
     }
     return true;
 }
Exemple #2
0
 /**
  * Return an instance of the database as a \PDO Object instance
  *
  * @return Current database instance (\PDO)
  */
 public static function getInstance()
 {
     // If the current database and the current config are null
     if (is_null(self::$db) && is_null(self::$config)) {
         // Getting the database config from the config file
         self::$config = Config::get('database');
         // Trying to create a \PDO object from the configuration
         try {
             $db = new \PDO('mysql:dbname=' . self::$config['database'] . ';host=' . self::$config['host'] . '', self::$config['username'], self::$config['password']);
         } catch (PDOException $e) {
             // If it failed, printing the error message
             echo $e->getMessage();
         }
         // Setting the current database
         self::$db = $db;
     }
     // Returning the current database
     return self::$db;
 }