<?php
class dbFunctions
{

	private $serverName, $username, $password, $conn, $database;

	public function __construct() {
		$this->dbFunctions();
	}

	public function dbFunctions() {
		$this->serverName = 'nordh.xyz';
		$this->username = 'odds';
		$this->password = 'czea_Wm0vK5vaJruM';
		$this->database = 'new_odds';
		$this->conn = NULL;
	}

	// Create Connection
	function connect() {
		if ($this->conn == NULL || ! $this->conn->ping()) {
			$this->conn = new mysqli($this->serverName, $this->username, $this->password, $this->database);
		}

		// Check connection
		if ($this->conn->connect_error) {
			die("Connection failed: " . $this->conn->connect_error);
		}
		mysqli_set_charset($this->conn, 'utf8');
	}

	public function getConnection() {
		if ($this->conn == NULL || ! $this->conn->ping()) {
			$this->connect();
		}
		return $this->conn;
	}

	function checkIfTableExists($tableName) {
		$result = $this->getSqlAsArray("SHOW TABLES LIKE '" . $leagueName . $this->tableName . "'");
		return ! empty($result);
	}

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

	function getSqlAsArray($sql) {
		$mysql = $this->getConnection();
		$result = $mysql->query($sql);
		if ($result === false) {
			echo $mysql->error . " from sql " . $sql . PHP_EOL;
			return null;
		}
		$returnArray = array();
		try {
			if (is_array($result) || is_object($result)) {
				foreach ($result as $key => $value) {
					$returnArray[$key] = $value;
				}
			} else {
				$returnArray[0] = "SUCCESS";
			}
		} catch (Exception $e) {
			echo __FILE__ . " " . $e->getMessage();
		}

		return $returnArray;
	}
}
