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
2022-01-16 17:12:04 +01:00

31 lines
799 B
PHP

<?php
// Encapsulates a single connection to a database
class DBHandle
{
private string $host = '127.0.0.1';
private string $db = 'test';
private string $user = 'root';
private string $pass = '';
private string $charset = 'utf8mb4';
public object $dbh;
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);
}
}