-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp.php
More file actions
32 lines (26 loc) · 831 Bytes
/
php.php
File metadata and controls
32 lines (26 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
// Type-like definition: Config container
class DBConfig {
const HOST = 'localhost';
const NAME = 'your_database_name';
const USER = 'your_username';
const PASS = 'your_password';
const CHARSET = 'utf8mb4';
}
// Function definition
function connectToDatabase(): PDO {
$dsn = "mysql:host=" . DBConfig::HOST . ";dbname=" . DBConfig::NAME . ";charset=" . DBConfig::CHARSET;
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
return new PDO($dsn, DBConfig::USER, DBConfig::PASS, $options);
} catch (PDOException $e) {
throw new PDOException($e->getMessage(), (int)$e->getCode());
}
}
// Usage
$pdo = connectToDatabase();
?>