-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.php
More file actions
79 lines (62 loc) · 1.89 KB
/
connection.php
File metadata and controls
79 lines (62 loc) · 1.89 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
// ------------------------------------
// 1. DbInfo Class (Credentials)
// ------------------------------------
class DbInfo {
protected string $host;
protected string $user;
protected string $pass;
public function __construct() {
// ✅ FIXED INSTANCE FORMAT
$this->host = "10.3.13.87"; // BEST for Laragon
$this->user = "sa";
$this->pass = "sa@123";
}
public function getHost(): string {
return $this->host;
}
public function getUser(): string {
return $this->user;
}
public function getPass(): string {
return $this->pass;
}
}
// ------------------------------------
// 2. Session Start
// ------------------------------------
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// ------------------------------------
// 3. Dbh Class (Connection Handler)
// ------------------------------------
class Dbh extends DbInfo {
private string $db_name = "inventoryuser";
protected function connect(): ?PDO {
$dsn = "sqlsrv:Server=" . $this->getHost() . ";Database=" . $this->db_name;
try {
$pdo = new PDO(
$dsn,
$this->getUser(),
$this->getPass(),
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]
);
return $pdo;
} catch (PDOException $e) {
// ✅ LOG ERROR
error_log("DB Connection Error: " . $e->getMessage());
// ✅ SHOW USER FRIENDLY MESSAGE
echo "Database connection failed. Please contact administrator.";
// ✅ VERY IMPORTANT RETURN NULL
return null;
}
}
public function getConnection(): ?PDO {
return $this->connect();
}
}