This commit is contained in:
nanguaboss
2026-08-03 00:07:01 +08:00
commit 71c6e8d9c1
112 changed files with 7815 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
<?php
/**
* PDO 数据库连接类(单例模式)
* 用法:$pdo = DB::getInstance()->getPdo();
*/
class DB
{
private static $instance = null;
private $pdo;
private function __construct()
{
$cfg = require __DIR__ . '/../../config/database.php';
$dsn = sprintf(
'mysql:host=%s;port=%d;dbname=%s;charset=%s',
$cfg['host'],
$cfg['port'],
$cfg['dbname'],
$cfg['charset']
);
$this->pdo = new PDO($dsn, $cfg['username'], $cfg['password'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
}
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function getPdo()
{
return $this->pdo;
}
// 禁止克隆
private function __clone() {}
}