1 Star 0 Fork 1

heli/简易订单处理系统

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Db.php 1.06 KB
一键复制 编辑 原始数据 按行查看 历史
heli 提交于 2024-05-12 16:52 +08:00 . '修改'
<?php
class Database {
private static $instance = null;
private $connection;
// 私有构造函数,防止外部实例化对象
private function __construct() {
// 连接数据库
$host = "127.0.0.1"; // 数据库主机
$dbname = "order_service"; // 数据库名称
$username = "root"; // 数据库用户名
$password = "root"; // 数据库密码
try {
$this->connection = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
// 如果连接失败,抛出异常
exit("Database connection failed: " . $e->getMessage());
}
}
// 获取数据库连接实例
public static function getInstance() {
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
// 获取数据库连接
public function getConnection() {
return $this->connection;
}
}
?>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/heli97/simple-order-processing-system.git
git@gitee.com:heli97/simple-order-processing-system.git
heli97
simple-order-processing-system
简易订单处理系统
master

搜索帮助