game/_incl_data/class/Core/Database.php

52 lines
1.3 KiB
PHP
Raw Normal View History

2022-12-19 20:22:19 +00:00
<?php
namespace Core;
use PDO;
class Database
{
2022-12-30 19:03:37 +00:00
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 utf8mb4');
2022-12-30 19:03:37 +00:00
}
2022-12-19 20:22:19 +00:00
2022-12-30 19:03:37 +00:00
/**
* Для совместимости со старыми функциями mysql_*.
* @return Database
*/
public static function init(): Database
{
if (!isset(self::$instance)) {
self::$instance = new static();
}
return self::$instance;
2022-12-19 20:22:19 +00:00
}
2022-12-30 19:03:37 +00:00
/**
* Новое подключение к БД.
* @return PDO
*/
2022-12-19 20:22:19 +00:00
public static function pdoinit(): PDO
{
return new PDO(
2022-12-30 19:03:37 +00:00
'mysql:host=localhost;dbname=' . Config::get('db_name'),
Config::get('db_user'),
Config::get('db_password'),
2022-12-19 20:22:19 +00:00
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4",
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false,
2022-12-19 20:22:19 +00:00
]
);
}
}