<?php

class Database {

	private $handle = null;

	public function __construct($host, $user, $password, $db) {
		$this->handle = new mysqli($host, $user, $password, $db);
		$this->handle->query("SET NAMES utf8;");
	}

	public function close() {
		$this->handle->Close();
	}

	public function selectQuery($query) {
		$stmt = $this->handle->query($query);

		if($stmt) {
			if($stmt->num_rows == 0) {
				$result = NULL;
			} else {
				$result = array();
				while($res = $stmt->fetch_object()) {
					$result[] = $res;
				}
			}
		} else {
			return null;
		}

		/*
		elseif($stmt->num_rows == 1) {
			$result = $stmt->fetch_object();
		}
		*/

		$stmt->close();
		return $result;
	}

	public function selectStringQuery($query) {
		$stmt = $this->handle->query($query);
		if($stmt) {
			if($stmt->num_rows == 0) {
				$result = NULL;
			} else {
				$result = $res = $stmt->fetch_array();
			}
		} else {
			return null;
		}

		/*
		elseif($stmt->num_rows == 1) {
			$result = $stmt->fetch_object();
		}
		*/

		$stmt->close();
		return $result[0];

	}

	public function insertQuery($query) {
		$stmt = $this->handle->prepare($query);
		$stmt->execute();
		$stmt->close();
	}

	public function updateQuery($query) {
		$stmt = $this->handle->prepare($query);
		if(!$stmt) {
			pa($this->handle);
		} else {
			$stmt->execute();
			$stmt->close();
		}
	}

	public function removeQuery($query) {
		$stmt = $this->handle->prepare($query);
		if(!$stmt) {
			pa($this->handle);
		} else {
			$stmt->execute();
			$stmt->close();
		}
	}
}

?>
