This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/app/core/DBHandle.php

31 lines
958 B
PHP
Raw Normal View History

2022-01-15 10:26:02 +00:00
<?php
// Encapsulates a single connection to a database
2022-01-16 16:12:04 +00:00
class DBHandle
2022-01-15 10:26:02 +00:00
{
2022-01-17 10:25:34 +00:00
private string $host = CONFIG['database']['args']['host'];
private string $db = CONFIG['database']['args']['database'];
private string $user = CONFIG['database']['args']['user'];
private string $pass = CONFIG['database']['args']['password'];
private string $charset = CONFIG['database']['args']['charset'];
2022-01-15 10:26:02 +00:00
2022-01-16 16:12:04 +00:00
public object $dbh;
2022-01-15 10:26:02 +00:00
public function __construct()
{
try {
$this->dbh = $this->connectWithMySQL();
} catch (PDOException $e) {
throw new PDOException($e->getMessage(), (int)$e->getCode());
}
}
private function connectWithMySQL(): object
{
$dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
$options = [
PDO::ATTR_PERSISTENT => true,
];
return new PDO($dsn, $this->user, $this->pass, $options);
}
}