From dae3964e7682dcd0d64075dfc28a23c12ef6c52e Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sat, 14 Jan 2017 02:26:28 -0500 Subject: Reset working directory for clean Scrott implementation --- examples/mysql.class.php | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 examples/mysql.class.php (limited to 'examples/mysql.class.php') diff --git a/examples/mysql.class.php b/examples/mysql.class.php new file mode 100644 index 0000000..90a4016 --- /dev/null +++ b/examples/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