-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
134 lines (114 loc) · 4.43 KB
/
index.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
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
<?php
class DB
{
private $con;
private $host;
private $user;
private $password;
private $database;
/**
* DB constructor.
*
* Initializes a new instance of the DB class, establishing a connection to the MySQL database
* using the provided host, user, password, and database name. If the connection fails, the script exits
* with an error message.
*
* @param string $host The hostname of the MySQL server.
* @param string $user The username for the MySQL connection.
* @param string $password The password for the MySQL connection.
* @param string $database The name of the MySQL database to connect to.
*/
public function __construct(string $host, string $user, string $password, string $database)
{
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->database = $database;
$this->con = new mysqli($this->host, $this->user, $this->password, $this->database);
if ($this->con->connect_error) {
exit($this->con->connect_error);
}
}
/**
* Executes a SQL query using a prepared statement with the given parameters.
*
* This function prepares and executes a SQL query using the MySQLi extension.
* It handles binding the provided parameters to the query, executing it, and returning the result object.
*
* @param string $query The SQL query to be executed.
* @param mixed ...$values The values for the query parameters for ? placeholders. **($arg1, $arg2, $arg3, ...)**
*
* @return mysqli_result|bool The result object if the query is successful, or false on failure.
*
* @throws RuntimeException If there is an error preparing or executing the statement, the function will output the error and terminate the script.
*/
public function query(string $query, ...$values)
{
$stmt = $this->con->prepare($query);
if (!$stmt) {
exit($this->con->error);
}
if (!empty($values)) {
$types = str_repeat("s", count($values));
$stmt->bind_param($types, ...$values);
}
if (!$stmt->execute()) {
exit($stmt->error);
}
$result = $stmt->get_result();
$stmt->close();
return $result;
}
/**
* Executes a stored procedure with the given name and parameters.
*
* This function constructs a SQL query to call a stored procedure using placeholders for the parameters.
* It then prepares and executes the query.
*
* @param string $name The name of the stored procedure to be executed.
* @param mixed ...$values The values for the stored procedure parameters. **($arg1, $arg2, $arg3, ...)**
*
* @return mixed The result of the stored procedure.
*
* @throws RuntimeException If there is an error preparing or executing the stored procedure, it'd throw an error
*/
public function procedure(string $name, ...$values)
{
$params = implode(',', array_fill(0, count($values), '?'));
$query = "CALL $name($params)";
$result = $this->query($query, ...$values);
return $result;
}
/**
* Fetches all rows from a MySQL result set and applies an optional callback to each row.
*
* This method iterates over the result set, fetches each row as an associative array, optionally applies
* a user-defined callback to each row, and collects the rows in an array.
*
* @param mysqli_result $result The result set returned from a MySQL query.
* @param callable|null $callback An optional callback function to apply to each row. The callback should
* accept a single parameter, which is the row array, and return the modified row array.
*
* @return array An array of associative arrays representing the rows in the result set. If the result set is empty
* or if the query failed, an empty array is returned.
*/
public function fetchAll($result, callable $callback = null)
{
$data = [];
if (!$result) {
return $data;
}
while ($row = $result->fetch_assoc()) {
if ($callback) {
$row = $callback($row);
}
$data[] = $row;
}
$result->free_result();
return $data;
}
public function __destruct()
{
$this->con->close();
}
}