23 lines
667 B
PHP
23 lines
667 B
PHP
|
<?php
|
||
|
// This is the first step towards actually doing work with the database
|
||
|
// Encapsulate a single connection to a particular database
|
||
|
|
||
|
Class DatabaseHandler {
|
||
|
public function connect(): object {
|
||
|
$host = '127.0.0.1';
|
||
|
$db = 'test';
|
||
|
$user = 'root';
|
||
|
$pass = '';
|
||
|
$charset = 'utf8mb4';
|
||
|
|
||
|
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
|
||
|
$options = [
|
||
|
PDO::ATTR_PERSISTENT => true,
|
||
|
];
|
||
|
try {
|
||
|
return new PDO($dsn, $user, $pass, $options);
|
||
|
} catch (PDOException $e) {
|
||
|
throw new PDOException($e->getMessage(), (int)$e->getCode());
|
||
|
}
|
||
|
}
|
||
|
}
|