database.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. class Database {
  3. private $handle = null;
  4. public function __construct($config) {
  5. $this->handle = new mysqli($config['dbHost'], $config['dbUser'], $config['dbPass'], $config['dbName']);
  6. $this->handle->set_charset("utf8");
  7. if($this->handle->connect_error) {
  8. echo "DB failed.";
  9. trigger_error('Database connection failed: ' . $this->handle->connect_error, E_USER_ERROR);
  10. exit(1);
  11. }
  12. }
  13. public function getString($what, $from, $where, $like) {
  14. $query = "SELECT `" . $what . "` FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  15. $res = $this->handle->query($query);
  16. if($res === false) {
  17. echo "DB failed.";
  18. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  19. exit(1);
  20. } else if($res->num_rows > 1 || $res->num_rows < 1) {
  21. echo "This shouldn't happen..1";
  22. exit(1);
  23. }
  24. return $res->fetch_row()[0];
  25. }
  26. public function getAllAssoc($from, $where = null, $like = null) {
  27. if($where && $like) {
  28. $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  29. } else {
  30. $query = "SELECT * FROM `" . $from . "`;";
  31. }
  32. $res = $this->handle->query($query);
  33. if($res === false) {
  34. echo "DB failed.";
  35. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  36. exit(1);
  37. } else if($res->num_rows < 1) {
  38. error_log($query);
  39. error_log("This shouldn't happen..2");
  40. //exit(1);
  41. }
  42. return $res->fetch_all(MYSQLI_ASSOC);
  43. }
  44. public function getAllAssocCustom($from, $custom, $where = null, $like = null) {
  45. if($where && $like) {
  46. $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "' " . $custom . ";";
  47. } else {
  48. $query = "SELECT * FROM `" . $from . "` " . $custom . ";";
  49. }
  50. $res = $this->handle->query($query);
  51. if($res === false) {
  52. echo "DB failed.";
  53. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  54. exit(1);
  55. } else if($res->num_rows < 1) {
  56. error_log("This shouldn't happen..3");
  57. //exit(1);
  58. }
  59. return $res->fetch_all(MYSQLI_ASSOC);
  60. }
  61. public function getAllRow($from, $where, $like) {
  62. $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  63. $res = $this->handle->query($query);
  64. if($res === false) {
  65. echo "DB failed.";
  66. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  67. exit(1);
  68. } else if($res->num_rows < 1) {
  69. error_log("This shouldn't happen..4");
  70. exit(1);
  71. }
  72. return $res->fetch_all(MYSQLI_NUM);
  73. }
  74. public function getAllRowCustom($from, $custom) {
  75. $query = "SELECT * FROM `" . $from . "` " . $custom . ";";
  76. $res = $this->handle->query($query);
  77. if($res === false) {
  78. echo "DB failed.";
  79. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  80. exit(1);
  81. } else if($res->num_rows < 1) {
  82. error_log("This shouldn't happen..5");
  83. //exit(1);
  84. }
  85. return $res->fetch_all(MYSQLI_NUM);
  86. }
  87. public function countRows($from, $where, $like) {
  88. $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  89. $res = $this->handle->query($query);
  90. if($res === false) {
  91. echo "DB failed.";
  92. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  93. exit(1);
  94. } else {
  95. return $res->num_rows;
  96. }
  97. }
  98. /*
  99. $cols = array(
  100. "moviedb-id",
  101. "name",
  102. "path",
  103. "poster",
  104. "backdrop",
  105. "overview"
  106. );
  107. $vals = array(
  108. "31295",
  109. "Misfits",
  110. "Misfits",
  111. "hia44dQ66CIfYPlLyaVcHRA9DtG.jpg",
  112. "m26kKegbYKyLvQZlctSh56j9KlO.jpg",
  113. "Misfits is a British science fiction comedy-drama television show, on [...]"
  114. );
  115. insertRow("series", $cols, $vals);
  116. */
  117. public function insertRow($into, $cols, $vals) {
  118. foreach($vals as $key => $val) {
  119. $vals[$key] = $this->handle->real_escape_string($val);
  120. }
  121. $colString = "(`" . implode('`, `', $cols) . "`)";
  122. $valString = "('" . implode("', '", $vals) . "')";
  123. $query = "INSERT INTO `" . $into . "` " . $colString . " VALUES " . $valString . ";";
  124. $res = $this->handle->query($query);
  125. if($res === false) {
  126. echo "DB failed.";
  127. trigger_error('Database failed: ' . $this->handle->error, E_USER_ERROR);
  128. exit(1);
  129. } else {
  130. return $this->handle->insert_id;
  131. }
  132. }
  133. public function deleteRows($from, $where, $like) {
  134. $query = "DELETE FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  135. $res = $this->handle->query($query);
  136. if($res === false) {
  137. echo "DB failed.";
  138. trigger_error('Database failed: ' . $this->handle->error, E_USER_ERROR);
  139. exit(1);
  140. } else {
  141. return true;
  142. }
  143. }
  144. public function updateRow($update, $col, $val, $where, $like) {
  145. $query = "UPDATE `" . $update . "` SET `" . $col . "` = " . $val . " WHERE `" . $where . "` LIKE '" . $like . "';";
  146. $res = $this->handle->query($query);
  147. if($res === false) {
  148. echo "DB failed.";
  149. trigger_error('Database failed: ' . $this->handle->error, E_USER_ERROR);
  150. error_log("Query: " . $query);
  151. exit(1);
  152. } else {
  153. return true;
  154. }
  155. }
  156. }