diff options
author | cflip <36554078+cflip@users.noreply.github.com> | 2021-04-24 09:40:20 -0600 |
---|---|---|
committer | cflip <36554078+cflip@users.noreply.github.com> | 2021-04-24 09:40:20 -0600 |
commit | 7c3f2e348c015ea93563d866f89ec8cea9159ea0 (patch) | |
tree | b7b6b18cf9087f42300f621d15101628a8d214e4 /includes/Database.php | |
parent | 6c9369ad85f2fb3dc61234b54db7e7079cdc0c4e (diff) |
Refactoring part 2
Starting to move some functionality such as the session and database connection into singleton classes to manage them. Functions for modifying posts and threads are being put in one place as well.
Diffstat (limited to 'includes/Database.php')
-rw-r--r-- | includes/Database.php | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/includes/Database.php b/includes/Database.php index 3308e4c..cdaa0f8 100644 --- a/includes/Database.php +++ b/includes/Database.php @@ -21,7 +21,7 @@ class Database } } - public static function get(): ?Database + public static function get() { if (self::$instance == null) { self::$instance = new Database(); @@ -30,8 +30,41 @@ class Database return self::$instance; } - public function query(string $sql) + public function query(string $sql, string $types = "", ...$vars): array { - mysqli_query($this->sql_connection, $sql); + $stmt = mysqli_stmt_init($this->sql_connection); + + if (!mysqli_stmt_prepare($stmt, $sql)) { + trigger_error('Could not create post due to internal error: ' . mysqli_error($this->sql_connection)); + } + + mysqli_stmt_bind_param($stmt, $types, ...$vars); + mysqli_stmt_execute($stmt); + + $result = array(); + $db_result = mysqli_stmt_get_result($stmt); + + if (mysqli_num_rows($db_result) > 0) { + while ($row = mysqli_fetch_assoc($db_result)) { + array_push($result, $row); + } + } + + mysqli_free_result($db_result); + mysqli_stmt_close($stmt); + + return $result; + } + + /** + * Returns the auto generated ID of the last query. + * This function is just a wrapper for mysqli_insert_id. + * In the future, it might be better to return different + * values in the query function depending on the type of + * SQL query. + */ + public function get_last_id() + { + return mysqli_insert_id($this->sql_connection); } }
\ No newline at end of file |