52 lines
1.3 KiB
PHP
52 lines
1.3 KiB
PHP
<?php
|
||
|
||
namespace Core;
|
||
|
||
use PDO;
|
||
|
||
class Database
|
||
{
|
||
private static self $instance;
|
||
protected static PDO $db;
|
||
|
||
/**
|
||
* Singleton.
|
||
*/
|
||
private function __construct() {
|
||
mysql_select_db(Config::get('db_name'), mysql_connect('localhost', Config::get('db_user'), Config::get('db_password')));
|
||
mysql_query('SET NAMES cp1251');
|
||
}
|
||
|
||
/**
|
||
* Для совместимости со старыми функциями mysql_*.
|
||
* @return Database
|
||
*/
|
||
public static function init(): Database
|
||
{
|
||
if (!isset(self::$instance)) {
|
||
self::$instance = new static();
|
||
}
|
||
return self::$instance;
|
||
}
|
||
|
||
/**
|
||
* Новое подключение к БД.
|
||
* @return PDO
|
||
*/
|
||
public static function pdoinit(): PDO
|
||
{
|
||
return new PDO(
|
||
'mysql:host=localhost;dbname=' . Config::get('db_name'),
|
||
Config::get('db_user'),
|
||
Config::get('db_password'),
|
||
[
|
||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES cp1251",
|
||
PDO::ATTR_STRINGIFY_FETCHES => false,
|
||
PDO::ATTR_EMULATE_PREPARES => false,
|
||
]
|
||
);
|
||
}
|
||
}
|