From bdc8790368e2f8b247c8492507d4083ddfbd61c1 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 17 Dec 2015 00:39:54 -0500 Subject: + Added generic database interface to use throughout the app since I'm planning on supporting multiple database engines + Defined interface for Mysql DBMS for Scrott --- app/class/mysql.class.php | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 app/class/mysql.class.php (limited to 'app/class/mysql.class.php') diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php new file mode 100644 index 0000000..b08257f --- /dev/null +++ b/app/class/mysql.class.php @@ -0,0 +1,63 @@ +db = new mysqli($host, $username, $password, $dbName); + + if ($this->db->connect_error) + throw new Exception("Can not connect to Mysql database. Please check your Scrott configuration."); + } + + /* + * Destructor + */ + function __destruct() + { + $this->close(); + } + + /* + * Close connection to DB + */ + function close() + { + $this->db->close(); + } + + /* + * Make a query of the database. Return data as an array of arrays + */ + function query($query) + { + $arr = array(); + $res = $this->db->query($query); + + if ($res === true || $res === false) + return $arr; + + foreach ($res as $r) + $arr[] = $r->fetch_assoc(); + + return $arr; + } + + /* + * Escape a string for use in a query + */ + function esc($string) + { + return $this->db->real_escape_string($string); + } +} + +?> -- cgit v1.2.3 From 0f9b65d812b601c5e047838b07b96098cbe8ad35 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 17 Dec 2015 13:21:49 -0500 Subject: * Bug fix in Mysql support class -- misuse of Mysql result object and its member function fetch_assoc --- app/class/mysql.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/class/mysql.class.php') diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php index b08257f..317468c 100644 --- a/app/class/mysql.class.php +++ b/app/class/mysql.class.php @@ -45,8 +45,8 @@ class Mysql implements Database if ($res === true || $res === false) return $arr; - foreach ($res as $r) - $arr[] = $r->fetch_assoc(); + while ($r = $res->fetch_assoc()) + $arr[] = $r; return $arr; } -- cgit v1.2.3