-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.php
66 lines (59 loc) · 1.79 KB
/
Database.php
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
require_once "config.php";
class Database
{
private $host = HOST;
private $dbname = DATABASE;
private $username = USERNAME;
private $password = PASSWORD;
private $port = PORT;
private $connection;
public function __construct()
{
try {
$this->connection = new PDO("pgsql:host={$this->host};port={$this->port};dbname={$this->dbname}", $this->username, $this->password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
}
public function execute($query, $params = [])
{
try {
$stmt = $this->connection->prepare($query);
$stmt->execute($params);
return true;
} catch (PDOException $e) {
die("Database query error: " . $e->getMessage());
}
}
public function query($query)
{
try {
$stmt = $this->connection->query($query);
return $stmt->fetchAll();
} catch (PDOException $e) {
die("Database query error: " . $e->getMessage());
}
}
public function queryParams($query, $params)
{
try {
$stmt = $this->connection->prepare($query);
$stmt->execute($params);
return $stmt->fetchAll();
} catch (PDOException $e) {
die("Database query error: " . $e->getMessage());
}
}
public function querySingle($query, $params)
{
try {
$stmt = $this->connection->prepare($query);
$stmt->execute($params);
return $stmt->fetchObject();
} catch (PDOException $e) {
die("Database query error: " . $e->getMessage());
}
}
}