database.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. class Database {
  3. private $handle = null;
  4. public static $db;
  5. public function __construct() {
  6. $this->handle = new mysqli(Config::$dbHost, Config::$dbUser, Config::$dbPass, Config::$dbName);
  7. $this->handle->set_charset("utf8");
  8. if($this->handle->connect_error) {
  9. ErrorHandler::$eh->addError("Database Error: Connection failed (" . $this->handle->connect_error . ")");
  10. }
  11. }
  12. public function executeQuery($query) {
  13. return $this->handle->query($query);
  14. }
  15. public function getString($what, $from, $where, $like) {
  16. if(gettype($where) == "array" && gettype($like) == "array") {
  17. $whereString = "";
  18. foreach($where as $key => $value) {
  19. if($key > 0) {
  20. $whereString .= " AND";
  21. }
  22. $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
  23. }
  24. $query = "SELECT `" . $what . "` FROM `" . $from . "` WHERE " . $whereString . ";";
  25. } else {
  26. $query = "SELECT `" . $what . "` FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  27. }
  28. $res = $this->handle->query($query);
  29. if($res === false) {
  30. ErrorHandler::$eh->addError("Database Error: " . $this->handle->error);
  31. return false;
  32. } else if($res->num_rows != 1) {
  33. ErrorHandler::$eh->addError("Database Error: Found more than one or less than one result in getString (" . $what . ")");
  34. return false;
  35. }
  36. return $res->fetch_row()[0];
  37. }
  38. public function getAllAssoc($from, $where = null, $like = null) {
  39. if(gettype($where) == "array" && gettype($like) == "array") {
  40. $whereString = "";
  41. foreach($where as $key => $value) {
  42. if($key > 0) {
  43. $whereString .= " AND";
  44. }
  45. $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
  46. }
  47. $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . ";";
  48. } else if($where && $like) {
  49. $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  50. } else {
  51. $query = "SELECT * FROM `" . $from . "`;";
  52. }
  53. $res = $this->handle->query($query);
  54. if($res === false) {
  55. echo "DB failed.";
  56. error_log("Failed query: " . $query . PHP_EOL);
  57. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  58. exit(1);
  59. } else if($res->num_rows < 1) {
  60. ErrorHandler::$eh->addError("Database error: no result (getAllAssoc)");
  61. //exit(1);
  62. }
  63. return $res->fetch_all(MYSQLI_ASSOC);
  64. }
  65. public function getAllAssocCustom($from, $custom, $where = null, $like = null) {
  66. if(gettype($where) == "array" && gettype($like) == "array") {
  67. $whereString = "";
  68. foreach($where as $key => $value) {
  69. if($key > 0) {
  70. $whereString .= " AND";
  71. }
  72. $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
  73. }
  74. $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . " " . $custom . ";";
  75. } else if($where && $like) {
  76. $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "' " . $custom . ";";
  77. } else {
  78. $query = "SELECT * FROM `" . $from . "` " . $custom . ";";
  79. }
  80. $res = $this->handle->query($query);
  81. if($res === false) {
  82. echo "DB failed.";
  83. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  84. exit(1);
  85. } else if($res->num_rows < 1) {
  86. error_log("This shouldn't happen..3");
  87. //exit(1);
  88. }
  89. return $res->fetch_all(MYSQLI_ASSOC);
  90. }
  91. public function getAllRow($from, $where = null, $like = null) {
  92. if(gettype($where) == "array" && gettype($like) == "array") {
  93. $whereString = "";
  94. foreach($where as $key => $value) {
  95. if($key > 0) {
  96. $whereString .= " AND";
  97. }
  98. $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
  99. }
  100. $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . ";";
  101. } else if($where && $like) {
  102. $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  103. } else {
  104. $query = "SELECT * FROM `" . $from . "`;";
  105. }
  106. $res = $this->handle->query($query);
  107. if($res === false) {
  108. echo "DB failed.";
  109. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  110. exit(1);
  111. } else if($res->num_rows < 1) {
  112. error_log("This shouldn't happen..4");
  113. exit(1);
  114. }
  115. return $res->fetch_all(MYSQLI_NUM);
  116. }
  117. public function getAllRowCustom($from, $custom) {
  118. if(gettype($where) == "array" && gettype($like) == "array") {
  119. $whereString = "";
  120. foreach($where as $key => $value) {
  121. if($key > 0) {
  122. $whereString .= " AND";
  123. }
  124. $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
  125. }
  126. $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . " " . $custom . ";";
  127. } else if($where && $like) {
  128. $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "' " . $custom . ";";
  129. } else {
  130. $query = "SELECT * FROM `" . $from . "` " . $custom . ";";
  131. }
  132. $res = $this->handle->query($query);
  133. if($res === false) {
  134. echo "DB failed.";
  135. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  136. exit(1);
  137. } else if($res->num_rows < 1) {
  138. error_log("This shouldn't happen..5");
  139. //exit(1);
  140. }
  141. return $res->fetch_all(MYSQLI_NUM);
  142. }
  143. public function countRows($from, $where = null, $like = null) {
  144. if(gettype($where) == "array" && gettype($like) == "array") {
  145. $whereString = "";
  146. foreach($where as $key => $value) {
  147. if($key > 0) {
  148. $whereString .= " AND";
  149. }
  150. $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
  151. }
  152. $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . ";";
  153. } else if($where && $like) {
  154. $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  155. } else {
  156. $query = "SELECT * FROM `" . $from . "`;";
  157. }
  158. $res = $this->handle->query($query);
  159. if($res === false) {
  160. echo "DB failed.";
  161. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  162. exit(1);
  163. } else {
  164. return $res->num_rows;
  165. }
  166. }
  167. public function insertRow($into, $cols, $vals) {
  168. foreach($vals as $key => $val) {
  169. $vals[$key] = $this->handle->real_escape_string($val);
  170. }
  171. $colString = "(`" . implode('`, `', $cols) . "`)";
  172. $valString = "('" . implode("', '", $vals) . "')";
  173. $query = "INSERT INTO `" . $into . "` " . $colString . " VALUES " . $valString . ";";
  174. $res = $this->handle->query($query);
  175. if($res === false) {
  176. echo "DB failed.";
  177. trigger_error('Database failed: ' . $this->handle->error, E_USER_ERROR);
  178. exit(1);
  179. } else {
  180. return $this->handle->insert_id;
  181. }
  182. }
  183. public function deleteRows($from, $where, $like) {
  184. if(gettype($where) == "array" && gettype($like) == "array") {
  185. $whereString = "";
  186. foreach($where as $key => $value) {
  187. if($key > 0) {
  188. $whereString .= " AND";
  189. }
  190. $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
  191. }
  192. $query = "DELETE FROM `" . $from . "` WHERE " . $whereString . ";";
  193. } else if($where && $like) {
  194. $query = "DELETE FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  195. }
  196. $res = $this->handle->query($query);
  197. if($res === false) {
  198. echo "DB failed.";
  199. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  200. exit(1);
  201. } else {
  202. return true;
  203. }
  204. }
  205. public function updateRow($update, $col, $val, $where, $like) {
  206. if(gettype($col) == "array" && gettype($val) == "array") {
  207. $setString = "SET";
  208. foreach($col as $key => $value) {
  209. if($key > 0) {
  210. $setString .= " ,";
  211. }
  212. $setString .= " `" . $value . "` = '" . $val[$key] . "'";
  213. }
  214. } else {
  215. $setString = " `" . $col . "` = '" . $val . "'";
  216. }
  217. if(gettype($where) == "array" && gettype($like) == "array") {
  218. $whereString = "";
  219. foreach($where as $key => $value) {
  220. if($key > 0) {
  221. $whereString .= " AND";
  222. }
  223. $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
  224. }
  225. } else if($where && $like) {
  226. $whereString = "`" . $where . "` LIKE '" . $like . "'";
  227. }
  228. $query = "UPDATE `" . $update . "` " . $setString . " WHERE " . $whereString . ";";
  229. $res = $this->handle->query($query);
  230. if($res === false) {
  231. echo "DB failed.";
  232. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  233. exit(1);
  234. } else {
  235. return true;
  236. }
  237. }
  238. }