<?php
class mysqliConnection {
var $host;
var $user;
var $pass;
var $dbName;
var $db;
public function __construct($host, $user, $pass, $db_name)
{
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->dbName = $db_name;
@$this->db = new mysqli($host, $user, $pass, $db_name);
if (mysqli_connect_errno()) {
system::error('Failed to connect to database');
}
$this->db->query('SET NAMES utf8');$this->db->query('SET CHARACTER SET utf8');
}
public function __destruct()
{
if (!mysqli_connect_errno()) {
$this->db->close();
}
}
/**
* @desc Create table if it doesn't exist
*/
public function checkTable($name, $arguments)
{
if ($this->query("SELECT * FROM $name") === false) {
$this->query("CREATE TABLE $name ($arguments)") ||
system::error("Couldn't create table $name: " . $this->db->error);
return false;
}
return true;
}
public function delete($table, $where)
{
return $this->query("DELETE FROM $table WHERE $where") or
system::error("Couldn't execute query: " . $this->db->error);
}
public function getRow($table, $columns = '*', $whereVar = null, $whereValue = null)
{
if (isset($whereVar) && isset($whereValue)) {
$where = "WHERE $whereVar = " . $this->quote($whereValue);
}
$sqlQuery = "SELECT $columns FROM $table $where";
$result = $this->query($sqlQuery) or
system::error("Couldn't execute query: " . $this->db->error);
return $result->fetch_assoc();
}
public function getRows($query)
{
$result = $this->query($query) or
system::error("Couldn't execute query: " . $this->db->error);
$returnArray = array();
while ($row = $result->fetch_assoc()) {
$returnArray[] = $row;
}
return $returnArray;
}
public function insert($table, $variables)
{
foreach ($variables as $var => $value) {
$vars[] = $var;
$values[] = $this->quote($value);
}
$sqlQuery = "INSERT INTO $table (" . implode($vars, ', ') . ") VALUES (". implode($values, ', ') . ")";
$this->query($sqlQuery) or
system::error("Couldn't execute query: " . $this->db->error);
return $this->lastInsertedId();
}
public function lastInsertedId()
{
return $this->db->insert_id;
}
public function update($table, $variables, $where)
{
foreach ($variables as $var => $value) {
$set[] = $var . "=" . $this->quote($value);
}
return $this->query("UPDATE $table SET " . implode($set, ', ') . " WHERE $where") or
system::error("Couldn't execute query: " . $this->db->error);
}
public function query($qstring)
{
return $this->db->query($qstring);
}
public function quote($value)
{
$value = ($value === true) ? 1 : $value;
$value = ($value === false) ? 0 : $value;
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
if (/*!is_numeric($value) && */!$this->_quoteSqlFunction($value)) {
$value = "'" . $this->db->real_escape_string($value) . "'";
}
return $value;
}
private function _quoteSqlFunction(&$value)
{
$sqlFunctions = array('NOW', 'FROM_UNIXTIME');
foreach ($sqlFunctions as $sqlFunction) {
if (preg_match("/^$sqlFunction\([^\)]*\)$/", $value, $matches) === 1) {
if (!empty($matches[1])) {
$value = "$sqlFunction(" . $this->quote($matches[1]) . ")";
}
return true;
}
}
return false;
}
}