Repository navigation
Expand file tree
/
Copy pathSmoothDB.php
More file actions
237 lines (203 loc) · 7.42 KB
/
Copy pathSmoothDB.php
File metadata and controls
237 lines (203 loc) · 7.42 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
<?php
class Database
{
private mysqli $conn;
// Connection variables
public function __construct(
string $host,
string $db,
string $user,
string $pass
) {
try {
// Connection to the database
$this->conn = new mysqli(
hostname: $host,
database: $db,
username: $user,
password: $pass
);
// Error handle upon failing to connect
if ($this->conn->connect_error) {
throw new Exception("Connection failed: " . $this->conn->connect_error);
}
} catch (Exception $error) {
// Catches the exception I threw earlier
echo $error->getMessage();
exit();
}
}
// Insertion function to the database
public function insert(
string $table,
array $data
): void {
// Checks if data is empty or not an array, otherwise returns false
if (empty($data)) {
echo "Data is empty";
return;
}
// Keys & Values of our db (Btw, in case you're curious: implode joins array elements into a string, like a comma in this case)
$columns = implode(", ", array_keys($data));
$placeHolder = implode(", ", array_fill(0, count($data), '?'));
$values = array_values($data);
// Since the class has to be dynamic, I have to check
// what type each value is and avoid assuming
$types = "";
foreach ($values as $i => $value) {
switch (gettype($value)) {
case "integer":
$types .= "i";
break;
// Will detect and convert both float & double to type 'd'
case "float":
case "double":
$types .= "d";
break;
case "string":
$types .= "s";
break;
case "boolean":
$types .= "i";
break;
default:
$types .= "s";
break;
}
// Converts boolean aka "True / False" Into their TINYINT
if (is_bool($value)) $values[$i] = $value ? 1 : 0;
}
try {
// Base Query
$query = "INSERT INTO $table ($columns) VALUES ($placeHolder)";
// Prepared statement
$stmt = $this->conn->prepare($query);
// Error checking if the statement failed to prepare
if (!$stmt) throw new Exception("Failed to prepare insert statement: " . $this->conn->error);
// bind, execute, and close the statement
$stmt->bind_param($types, ...$values);
// Error checking if the statement failed to execute
if (!$stmt->execute()) throw new Exception("Failed to execute insert statement: " . $stmt->error);
// Closing connection
$stmt->close();
} catch (Exception $error) {
echo "<h1>Database Insertion Error</h1> " . $error->getMessage();
exit();
}
}
// Won't re-comment similar code (prepared statements, binding, executing etc)
public function fetch(
string $table,
string $columns = "*",
string $item = "",
array $params = []
): array {
try {
$query = "SELECT $columns FROM $table";
if (!empty($item)) {
$query .= " WHERE $item";
}
$stmt = $this->conn->prepare($query);
if (!$stmt) throw new Exception("Failed to prepare fetch statement statement: " . $this->conn->error);
if (!empty($params)) {
$types = "";
foreach ($params as &$param) {
switch (gettype($param)) {
case "integer":
$types .= "i";
break;
case "float":
case "double":
$types .= "d";
break;
case "string":
$types .= "s";
break;
case "boolean":
$types .= "i";
$param = $param ? 1 : 0;
break;
default:
$types .= "s";
break;
}
}
$stmt->bind_param($types, ...$params);
}
if (!$stmt->execute()) throw new Exception("Failed to execute fetch statement: " . $stmt->error);
// Now to get results
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $data;
} catch (Exception $error) {
echo "Fetch Error: " . $error->getMessage();
return [];
}
}
// Start updating
public function update(
string $table,
array $columns = [],
string $condition,
string $param
) {
try {
// Query of MYSQL to update existing row
$query = "UPDATE $table SET $columns WHERE $condition";
$stmt = $this->conn->prepare($query);
if (!$stmt) throw new Exception("Failed to prepare update statement");
if (!empty($params)) {
$types = "";
foreach ($params as &$param) {
switch (gettype($param)) {
case "integer":
$types .= "i";
break;
case "float":
case "double":
$types .= "d";
break;
case "string":
$types .= "s";
break;
case "boolean":
$types .= "i";
$param = $param ? 1 : 0;
break;
default:
$types .= "s";
break;
}
}
$stmt->bind_param($types, ...$params);
if (!$stmt->execute()) throw new Exception("Couldn't exectue update statement");
$stmt->close();
}
} catch (Exception $error) {
echo "Update Error:" . $error->getMessage();
exit();
}
}
public function delete(
string $table,
string $item,
string $operator,
array $params = []
) {
try {
$query = "DELETE FROM $table WHERE $item $operator ?";
$stmt = $this->conn->prepare($query);
if (!$stmt) throw new Exception("Failed to prepare delete statement" . $this->conn->error);
if (!empty($params)) {
$types = str_repeat("s", count($params));
$stmt->bind_param($types, ...$params);
}
if (!$stmt->execute()) throw new Exception("Failed to execute delete statement" . $stmt->error);
$stmt->close();
} catch (Exception $error) {
echo "Deletion Error:" . $error->getMessage();
exit();
}
}
}