| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- class Database {
- private $handle = null;
- public static $db;
- public function __construct() {
- $this->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;
- }
- }
- }
|