DB.php 633 B

1234567891011121314151617181920212223
  1. <?php
  2. namespace inter\storage\mysql;
  3. use Exception;
  4. use PDO;
  5. use PDOException;
  6. class DB extends PDO
  7. {
  8. private Config $config;
  9. public function __construct(Config $config)
  10. {
  11. $this->config = $config;
  12. try {
  13. // 连接数据库
  14. parent::__construct($this->config->GetPdoDSN(), $this->config->getUser(), $this->config->getPassword());
  15. // 设置错误模式为异常
  16. $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  17. } catch (PDOException $e) {
  18. throw new Exception("连接数据库失败: " . $e->getMessage());
  19. }
  20. }
  21. }