-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbh.php
More file actions
242 lines (213 loc) · 7.21 KB
/
Dbh.php
File metadata and controls
242 lines (213 loc) · 7.21 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
class Dbh
{
protected $conn;
private $host = "localhost";
private $dbName = "hospital";
public function __construct()
{
try {
//code...
$this->conn = new mysqli($this->host, 'root', '', $this->dbName);
if ($this->conn->connect_error) {
die("Connection failed: " . $this->conn->connect_error);
}
// $this->createPatientTable();
// $this->createDoctorTable();
} catch (RuntimeException $e) {
//throw $th;
echo $e->getMessage();
}
}
public function __destruct()
{
$this->conn->close();
}
private function checkForConnection()
{
echo var_dump($this->conn) . "<br>";
}
public function getConnection()
{
return $this->conn;
}
public function query($sql, $bind) {
$stmt = $this->conn->prepare($sql);
// Execute the statement
if (!$stmt) {
echo "EMPTY QUERY<br>";
return null;
}
if ($bind) {
// Bind parameters to the statement
foreach ($bind as $type => $values) {
foreach ($values as $value) {
$stmt->bind_param($type, $value);
}
}
}
$stmt->execute();
// Get the results
$result = $stmt->get_result();
// Fetch all rows
if ($result) {
$results = $result->fetch_all(MYSQLI_ASSOC);
}
// Free result and close statement
$stmt->close();
return $results;
}
// dbName => component
public function select($table, $items = '*', $where = null, $allFlag = false)
{
$sql = 'SELECT ' . $items . ' FROM ' . $table;
if ($where !== null) {
$sql .= ' WHERE';
$firstDBname = array_key_first($where);
foreach ($where as $nameInDB => $component) {
$sql .= ($nameInDB == $firstDBname ? '' : 'AND') . ' ' . $nameInDB . ' = "' . $component . '" ';
}
}
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
if ($result->num_rows > 0) {
$row = ($allFlag) ? $result->fetch_all(MYSQLI_ASSOC) : $result->fetch_assoc();
return $row;
}
return false;
}
//FOR INSERT REMEMBER dbName => component
public function insert($table, $items)
{
$sql = 'INSERT INTO ' . $table . ' (';
if (isset($items)) {
$keys = array_keys($items);
$sql .= $keys[0];
for ($i = 1; $i < count($keys); $i++) {
$sql .= ', ' . $keys[$i];
}
$sql .= ') VALUES (';
$values = array_values($items);
$sql .= '"' . $values[0] . '"';
for ($i = 1; $i < count($values); $i++) {
$sql .= ', ' . '"' . $values[$i] . '"';;
}
$sql .= ')';
} else {
die("No Items to insert<br>");
}
if (isset($this->conn)) {
$this->conn->query($sql);
} else {
die("Cant insert<br>");
}
}
public function resetTable($table) {
$this->checkForConnection();
$sql = 'TRUNCATE TABLE '.$table;
if(isset($this->conn)) {
$this->conn->query($sql);
} else {
die("Cant truncate<br>");
}
}
public function delete($table, $item) {
$this->checkForConnection();
$sql = 'DELETE FROM '.$table.' WHERE';
if ($item !== null) {
$flag = true;
$firstDBname = array_key_first($item);
foreach ($item as $nameInDB => $component) {
if($component == "" || $component == "No") {
continue;
} else if ($flag) {
$firstDBname = $nameInDB;
$flag = false;
}
$sql .= ($nameInDB == $firstDBname ? '' : 'AND') . ' ' . $nameInDB . ' = "' . $component . '" ';
}
$stmt = $this->conn->prepare($sql);
$stmt->execute();
}
}
public function updateProfile($table, $colum, $condition ,$id) {
// CHỌN 1 CÁI
$sql = "UPDATE ".$table." SET ".$colum." = "."'".$condition."'"." WHERE ID = "."'".$id."'";
echo var_dump($sql)."<br>";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
}
// nameInDB => amount
public function updateAmount($table, $items, $where)
{
// Initialize SQL query
$sql = 'UPDATE ' . $table . ' SET ';
$updates = [];
// Construct the SET clause
foreach ($items as $nameInDb => $amount) {
$updates[] = $nameInDb . ' = ' . $nameInDb . ' + ?';
}
$sql .= implode(', ', $updates);
echo "after set: ".var_dump($sql)."<br>";
// Construct the WHERE clause
$whereClause = [];
foreach ($where as $keyInDb => $key) {
$whereClause[] = $keyInDb . ' = ?';
}
$sql .= ' WHERE ' . implode(' AND ', $whereClause);
echo "after WHERE: ".var_dump($sql)."<br>";
// Prepare and bind parameters
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
echo "Error preparing statement: " . $this->conn->error;
return;
}
$types = str_repeat('s', count($items) + count($where)); // Assuming all parameters are strings
$params = array_merge(array_values($items), array_values($where));
$stmt->bind_param($types, ...$params);
// Execute the statement
if ($stmt->execute()) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $stmt->error;
}
$stmt->close();
}
public function createPatientTable()
{
$sql = "CREATE TABLE IF NOT EXISTS patients (
ID INT(10),
fName VARCHAR(30),
lName VARCHAR(30),
email VARCHAR(50) not null,
phoneNum VARCHAR(15),
gender ENUM('M', 'F'),
pw VARCHAR(16) not null,
PRIMARY KEY (ID)
)";
$this->conn->query($sql);
}
public function createDoctorTable()
{
// tạo một cái bảng name staffs
// ID để phân biệt giữa các nhân viên y tế (BẮT BUỘC: PRIMARY KEY)
$sql = "CREATE TABLE IF NOT EXISTS staffs (
ID VARCHAR(10),
staffUserName VARCHAR(50),
fname VARCHAR(30),
lname VARCHAR(30),
email VARCHAR(50),
prof INT(2),
staffPassword VARCHAR(16),
gender ENUM('Male', 'Female'),
task ENUM('Doctor', 'Nurse', 'Other'),
startDate DATE,
phoneNumber VARCHAR(11),
annualLeaveDay INT(2) DEFAULT(12),
PRIMARY KEY (ID)
)";
$this->conn->query($sql);
}
}