database.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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("This shouldn't happen..2");
  39. //exit(1);
  40. }
  41. return $res->fetch_all(MYSQLI_ASSOC);
  42. }
  43. public function getAllAssocCustom($from, $custom, $where = null, $like = null) {
  44. if($where && $like) {
  45. $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "' " . $custom . ";";
  46. } else {
  47. $query = "SELECT * FROM `" . $from . "` " . $custom . ";";
  48. }
  49. $res = $this->handle->query($query);
  50. if($res === false) {
  51. echo "DB failed.";
  52. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  53. exit(1);
  54. } else if($res->num_rows < 1) {
  55. error_log("This shouldn't happen..3");
  56. //exit(1);
  57. }
  58. return $res->fetch_all(MYSQLI_ASSOC);
  59. }
  60. public function getAllRow($from, $where, $like) {
  61. $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  62. $res = $this->handle->query($query);
  63. if($res === false) {
  64. echo "DB failed.";
  65. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  66. exit(1);
  67. } else if($res->num_rows < 1) {
  68. error_log("This shouldn't happen..4");
  69. exit(1);
  70. }
  71. return $res->fetch_all(MYSQLI_NUM);
  72. }
  73. public function getAllRowCustom($from, $custom) {
  74. $query = "SELECT * FROM `" . $from . "` " . $custom . ";";
  75. $res = $this->handle->query($query);
  76. if($res === false) {
  77. echo "DB failed.";
  78. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  79. exit(1);
  80. } else if($res->num_rows < 1) {
  81. error_log("This shouldn't happen..5");
  82. //exit(1);
  83. }
  84. return $res->fetch_all(MYSQLI_NUM);
  85. }
  86. public function countRows($from, $where, $like) {
  87. $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  88. $res = $this->handle->query($query);
  89. if($res === false) {
  90. echo "DB failed.";
  91. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  92. exit(1);
  93. } else {
  94. return $res->num_rows;
  95. }
  96. }
  97. /*
  98. $cols = array(
  99. "moviedb-id",
  100. "name",
  101. "path",
  102. "poster",
  103. "backdrop",
  104. "overview"
  105. );
  106. $vals = array(
  107. "31295",
  108. "Misfits",
  109. "Misfits",
  110. "hia44dQ66CIfYPlLyaVcHRA9DtG.jpg",
  111. "m26kKegbYKyLvQZlctSh56j9KlO.jpg",
  112. "Misfits is a British science fiction comedy-drama television show, on [...]"
  113. );
  114. insertRow("series", $cols, $vals);
  115. */
  116. public function insertRow($into, $cols, $vals) {
  117. foreach($vals as $key => $val) {
  118. $vals[$key] = $this->handle->real_escape_string($val);
  119. }
  120. $colString = "(`" . implode('`, `', $cols) . "`)";
  121. $valString = "('" . implode("', '", $vals) . "')";
  122. $query = "INSERT INTO `" . $into . "` " . $colString . " VALUES " . $valString . ";";
  123. $res = $this->handle->query($query);
  124. if($res === false) {
  125. echo "DB failed.";
  126. trigger_error('Database failed: ' . $this->handle->error, E_USER_ERROR);
  127. exit(1);
  128. } else {
  129. return $this->handle->insert_id;
  130. }
  131. }
  132. public function deleteRows($from, $where, $like) {
  133. $query = "DELETE FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
  134. $res = $this->handle->query($query);
  135. if($res === false) {
  136. echo "DB failed.";
  137. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  138. exit(1);
  139. } else {
  140. return true;
  141. }
  142. }
  143. public function updateRow($update, $col, $val, $where, $like) {
  144. $query = "UPDATE `" . $update . "` SET `" . $col . "` = " . $val . " WHERE `" . $where . "` LIKE '" . $like . "';";
  145. $res = $this->handle->query($query);
  146. if($res === false) {
  147. echo "DB failed.";
  148. trigger_error('Database failed: ' . $this->handle->connect_error, E_USER_ERROR);
  149. exit(1);
  150. } else {
  151. return true;
  152. }
  153. }
  154. }