diff options
author | cflip <36554078+cflip@users.noreply.github.com> | 2021-06-23 15:21:12 -0600 |
---|---|---|
committer | cflip <36554078+cflip@users.noreply.github.com> | 2021-06-23 15:21:12 -0600 |
commit | f5e972c030675f46cda543e13da1b787457e070b (patch) | |
tree | 7c0d2bb4138fe60060ff73b61e15881765ba5412 /includes/Database.php | |
parent | fd0c3a283153d6f2d759e5e14888e40e65dc61b7 (diff) |
Add the rest of the changes
Diffstat (limited to 'includes/Database.php')
-rwxr-xr-x[-rw-r--r--] | includes/Database.php | 162 |
1 files changed, 81 insertions, 81 deletions
diff --git a/includes/Database.php b/includes/Database.php index 0a79dfb..61fbbb1 100644..100755 --- a/includes/Database.php +++ b/includes/Database.php @@ -1,82 +1,82 @@ -<?php - -class Database -{ - private static $instance = null; - private $sql_connection; - - private function __construct() - { - $config = parse_ini_file('/var/www/config.ini', true)['mysql_credentials']; - - $db_server = $config['server']; - $db_user = $config['user']; - $db_pass = $config['password']; - $db_database = $config['database']; - - $this->sql_connection = mysqli_connect($db_server, $db_user, $db_pass, $db_database); - - if (!$this->sql_connection) { - trigger_error("Database connection error: " . mysqli_connect_error()); - } - } - - public static function get() - { - if (self::$instance == null) { - self::$instance = new Database(); - } - - return self::$instance; - } - - public function query(string $sql, string $types = "", ...$vars): array - { - $result = array(); - - if ($types == "") { - // No types were provided, preparing a statement is not necessary - $db_result = mysqli_query($this->sql_connection, $sql); - } else { - $stmt = mysqli_stmt_init($this->sql_connection); - - if (!mysqli_stmt_prepare($stmt, $sql)) { - trigger_error('Internal error: ' . mysqli_error($this->sql_connection)); - return $result; - } - - mysqli_stmt_bind_param($stmt, $types, ...$vars); - mysqli_stmt_execute($stmt); - - $db_result = mysqli_stmt_get_result($stmt); - - mysqli_stmt_close($stmt); - } - - if (!$db_result) { - return $result; - } - - if (mysqli_num_rows($db_result) > 0) { - while ($row = mysqli_fetch_assoc($db_result)) { - array_push($result, $row); - } - } - - mysqli_free_result($db_result); - - 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); - } +<?php
+
+class Database
+{
+ private static $instance = null;
+ private $sql_connection;
+
+ private function __construct()
+ {
+ $config = parse_ini_file('config.ini', true)['mysql_credentials'];
+
+ $db_server = $config['server'];
+ $db_user = $config['user'];
+ $db_pass = $config['password'];
+ $db_database = $config['database'];
+
+ $this->sql_connection = mysqli_connect($db_server, $db_user, $db_pass, $db_database);
+
+ if (!$this->sql_connection) {
+ trigger_error("Database connection error: " . mysqli_connect_error());
+ }
+ }
+
+ public static function get()
+ {
+ if (self::$instance == null) {
+ self::$instance = new Database();
+ }
+
+ return self::$instance;
+ }
+
+ public function query(string $sql, string $types = "", ...$vars): array
+ {
+ $result = array();
+
+ if ($types == "") {
+ // No types were provided, preparing a statement is not necessary
+ $db_result = mysqli_query($this->sql_connection, $sql);
+ } else {
+ $stmt = mysqli_stmt_init($this->sql_connection);
+
+ if (!mysqli_stmt_prepare($stmt, $sql)) {
+ trigger_error('Internal error: ' . mysqli_error($this->sql_connection));
+ return $result;
+ }
+
+ mysqli_stmt_bind_param($stmt, $types, ...$vars);
+ mysqli_stmt_execute($stmt);
+
+ $db_result = mysqli_stmt_get_result($stmt);
+
+ mysqli_stmt_close($stmt);
+ }
+
+ if (!$db_result) {
+ return $result;
+ }
+
+ if (mysqli_num_rows($db_result) > 0) {
+ while ($row = mysqli_fetch_assoc($db_result)) {
+ array_push($result, $row);
+ }
+ }
+
+ mysqli_free_result($db_result);
+
+ 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 |