| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- class Database {
- private $handle = null;
- public function __construct($config) {
- $this->handle = new mysqli($config['dbHost'], $config['dbUser'], $config['dbPass'], $config['dbName']);
- $this->handle->set_charset("utf8");
- if($this->handle->connect_error) {
- echo "DB failed.";
- trigger_error('Database connection failed: ' . $this->handle->connect_error, E_USER_ERROR);
- exit(1);
- }
- }
- public function getString($what, $from, $where, $like) {
- $query = "SELECT `" . $what . "` 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 if($res->num_rows > 1 || $res->num_rows < 1) {
- echo "This shouldn't happen..1";
- exit(1);
- }
- return $res->fetch_row()[0];
- }
- public function getAllAssoc($from, $where = null, $like = null) {
- 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($query);
- error_log("This shouldn't happen..2");
- //exit(1);
- }
- return $res->fetch_all(MYSQLI_ASSOC);
- }
- public function getAllAssocCustom($from, $custom, $where = null, $like = null) {
- 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, $like) {
- $query = "SELECT * 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 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) {
- $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, $like) {
- $query = "SELECT * 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 $res->num_rows;
- }
- }
- /*
- $cols = array(
- "moviedb-id",
- "name",
- "path",
- "poster",
- "backdrop",
- "overview"
- );
- $vals = array(
- "31295",
- "Misfits",
- "Misfits",
- "hia44dQ66CIfYPlLyaVcHRA9DtG.jpg",
- "m26kKegbYKyLvQZlctSh56j9KlO.jpg",
- "Misfits is a British science fiction comedy-drama television show, on [...]"
- );
- insertRow("series", $cols, $vals);
- */
- 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) {
- $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->error, E_USER_ERROR);
- exit(1);
- } else {
- return true;
- }
- }
- public function updateRow($update, $col, $val, $where, $like) {
- $query = "UPDATE `" . $update . "` SET `" . $col . "` = " . $val . " WHERE `" . $where . "` LIKE '" . $like . "';";
- $res = $this->handle->query($query);
- if($res === false) {
- echo "DB failed.";
- trigger_error('Database failed: ' . $this->handle->error, E_USER_ERROR);
- error_log("Query: " . $query);
- exit(1);
- } else {
- return true;
- }
- }
- }
|