From b6f82bb6552517d8bc442a2087c6c37a33bd18bd Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sun, 23 Oct 2016 19:01:52 -0400 Subject: Add mysql class --- app/class/database.class.php | 1 + app/class/mysql.class.php | 74 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 app/class/mysql.class.php (limited to 'app/class') diff --git a/app/class/database.class.php b/app/class/database.class.php index c791088..6c6ecd6 100644 --- a/app/class/database.class.php +++ b/app/class/database.class.php @@ -23,6 +23,7 @@ abstract class Database { private static $instance = NULL; + protected $db; /* * Return the database instance object, creating it if this is the diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php new file mode 100644 index 0000000..90a4016 --- /dev/null +++ b/app/class/mysql.class.php @@ -0,0 +1,74 @@ +db = new mysqli($host, $username, $password, $dbName); + + if ($this->db->connect_error) + throw new Exception("Can not connect to MySQL database. Please check your configuration."); + } + + /* + * Destructor + */ + public function __destruct() + { + $this->close(); + } + + /* + * Close connection to DB + */ + public function close() + { + $this->db->close(); + } + + /* + * Make a query of the database. Return data as an array of arrays. + */ + public function query(string $query) : array + { + $arr = array(); + $res = $this->db->query($query); + + if ($res === true || $res === false) + return $arr; + + while (($arr[] = $res->fetch_assoc())); + return $arr; + } + + /* + * Escape a string for use in a query + */ + public function esc(string $str) : string + { + return $this->db->real_escape_string($str); + } +} + +?> -- cgit v1.2.3