game/_incl_data/class/Core/Database.php
2022-12-30 21:03:50 +02:00

50 lines
1.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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",
]
);
}
}