-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.php
More file actions
48 lines (42 loc) · 1.26 KB
/
Copy pathDatabase.php
File metadata and controls
48 lines (42 loc) · 1.26 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
class Database {
private string $username;
private string $password;
private string $host;
private string $database;
private string $port;
private ?PDO $conn = null;
public function __construct(
string $username,
string $password,
string $host,
string $database,
string $port
) {
$this->username = $username;
$this->password = $password;
$this->host = $host;
$this->database = $database;
$this->port = $port;
}
public function connect() {
if ($this->conn === NULL) {
try {
$this->conn = new PDO(
"pgsql:host=$this->host;port=$this->port;dbname=$this->database",
$this->username,
$this->password,
["sslmode" => "prefer"]
);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $this->conn;
} catch(PDOException $e) {
throw new RuntimeException('Could not connect to the database.', previous: $e);
}
}
return $this->conn;
}
public function disconnect(): void {
$this->conn = null;
}
}