handle = new mysqli(Config::$dbHost, Config::$dbUser, Config::$dbPass, Config::$dbName); $this->handle->set_charset("utf8"); if($this->handle->connect_error) { ErrorHandler::$eh->addError("Database Error: Connection failed (" . $this->handle->connect_error . ")"); } } public function executeQuery($query) { return $this->handle->query($query); } public function getString($what, $from, $where, $like) { if(gettype($where) == "array" && gettype($like) == "array") { $whereString = ""; foreach($where as $key => $value) { if($key > 0) { $whereString .= " AND"; } $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'"; } $query = "SELECT `" . $what . "` FROM `" . $from . "` WHERE " . $whereString . ";"; } else { $query = "SELECT `" . $what . "` FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';"; } $res = $this->handle->query($query); if($res === false) { ErrorHandler::$eh->addError("Database Error: " . $this->handle->error); return false; } else if($res->num_rows != 1) { ErrorHandler::$eh->addError("Database Error: Found more than one or less than one result in getString (" . $what . ")"); return false; } return $res->fetch_row()[0]; } public function getAllAssoc($from, $where = null, $like = null) { if(gettype($where) == "array" && gettype($like) == "array") { $whereString = ""; foreach($where as $key => $value) { if($key > 0) { $whereString .= " AND"; } $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'"; } $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . ";"; } else if($where && $like) { $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';"; } else { $query = "SELECT * FROM `" . $from . "`;"; } $res = $this->handle->query($query); if($res === false) { echo "DB failed."; error_log("Failed query: " . $query . PHP_EOL); trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR); exit(1); } else if($res->num_rows < 1) { ErrorHandler::$eh->addError("Database error: no result (getAllAssoc)"); //exit(1); } return $res->fetch_all(MYSQLI_ASSOC); } public function getAllAssocCustom($from, $custom, $where = null, $like = null) { if(gettype($where) == "array" && gettype($like) == "array") { $whereString = ""; foreach($where as $key => $value) { if($key > 0) { $whereString .= " AND"; } $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'"; } $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . " " . $custom . ";"; } else if($where && $like) { $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "' " . $custom . ";"; } else { $query = "SELECT * FROM `" . $from . "` " . $custom . ";"; } $res = $this->handle->query($query); if($res === false) { echo "DB failed."; trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR); exit(1); } else if($res->num_rows < 1) { error_log("This shouldn't happen..3"); //exit(1); } return $res->fetch_all(MYSQLI_ASSOC); } public function getAllRow($from, $where = null, $like = null) { if(gettype($where) == "array" && gettype($like) == "array") { $whereString = ""; foreach($where as $key => $value) { if($key > 0) { $whereString .= " AND"; } $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'"; } $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . ";"; } else if($where && $like) { $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';"; } else { $query = "SELECT * FROM `" . $from . "`;"; } $res = $this->handle->query($query); if($res === false) { echo "DB failed."; trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR); exit(1); } else if($res->num_rows < 1) { error_log("This shouldn't happen..4"); exit(1); } return $res->fetch_all(MYSQLI_NUM); } public function getAllRowCustom($from, $custom) { if(gettype($where) == "array" && gettype($like) == "array") { $whereString = ""; foreach($where as $key => $value) { if($key > 0) { $whereString .= " AND"; } $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'"; } $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . " " . $custom . ";"; } else if($where && $like) { $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "' " . $custom . ";"; } else { $query = "SELECT * FROM `" . $from . "` " . $custom . ";"; } $res = $this->handle->query($query); if($res === false) { echo "DB failed."; trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR); exit(1); } else if($res->num_rows < 1) { error_log("This shouldn't happen..5"); //exit(1); } return $res->fetch_all(MYSQLI_NUM); } public function countRows($from, $where = null, $like = null) { if(gettype($where) == "array" && gettype($like) == "array") { $whereString = ""; foreach($where as $key => $value) { if($key > 0) { $whereString .= " AND"; } $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'"; } $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . ";"; } else if($where && $like) { $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';"; } else { $query = "SELECT * FROM `" . $from . "`;"; } $res = $this->handle->query($query); if($res === false) { echo "DB failed."; trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR); exit(1); } else { return $res->num_rows; } } public function insertRow($into, $cols, $vals) { foreach($vals as $key => $val) { $vals[$key] = $this->handle->real_escape_string($val); } $colString = "(`" . implode('`, `', $cols) . "`)"; $valString = "('" . implode("', '", $vals) . "')"; $query = "INSERT INTO `" . $into . "` " . $colString . " VALUES " . $valString . ";"; $res = $this->handle->query($query); if($res === false) { echo "DB failed."; trigger_error('Database failed: ' . $this->handle->error, E_USER_ERROR); exit(1); } else { return $this->handle->insert_id; } } public function deleteRows($from, $where, $like) { if(gettype($where) == "array" && gettype($like) == "array") { $whereString = ""; foreach($where as $key => $value) { if($key > 0) { $whereString .= " AND"; } $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'"; } $query = "DELETE FROM `" . $from . "` WHERE " . $whereString . ";"; } else if($where && $like) { $query = "DELETE FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';"; } $res = $this->handle->query($query); if($res === false) { echo "DB failed."; trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR); exit(1); } else { return true; } } public function updateRow($update, $col, $val, $where, $like) { if(gettype($col) == "array" && gettype($val) == "array") { $setString = "SET"; foreach($col as $key => $value) { if($key > 0) { $setString .= " ,"; } $setString .= " `" . $value . "` = '" . $val[$key] . "'"; } } else { $setString = " `" . $col . "` = '" . $val . "'"; } if(gettype($where) == "array" && gettype($like) == "array") { $whereString = ""; foreach($where as $key => $value) { if($key > 0) { $whereString .= " AND"; } $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'"; } } else if($where && $like) { $whereString = "`" . $where . "` LIKE '" . $like . "'"; } $query = "UPDATE `" . $update . "` " . $setString . " WHERE " . $whereString . ";"; $res = $this->handle->query($query); if($res === false) { echo "DB failed."; trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR); exit(1); } else { return true; } } }