<?php
class mysqlClass
{
	var $_conn = ''; //Stores the connection string
	var $_error = '';
	var $_result = '';
	public function __construct(){
		$this->mysqlClass();
	}
	function mysqlClass()
	{
		global $dbhost, $dbuser, $dbpassword, $dbname, $dbprefix;
		return $this->_conn = mysqli_connect($dbhost, $dbuser, $dbpassword,$dbname);
	}
	
	function setQuery($query,$noreplace=false)
	{
		$query =  trim($query);
		global $dbprefix;
		if(!$noreplace)
		{
			$query = str_replace('##', $dbprefix, $query);
		}
		return $query;
	}

	function query($query,$noreplace=false)
	{
		global $sqlObj;
		$conntion = $sqlObj->mysqlClass();
		$query = $this->setQuery($query,$noreplace);
		$this->_result = mysqli_query($conntion,$query) or $this->setError($query, 'Error : '.mysqli_error($conntion).' : '.$query);
		if(strtoupper(substr($query, 0, 6)) == 'INSERT')
		{
			return mysqli_insert_id();
		}
	}
	
	function iQuery($query,$noreplace=false)
	{
		global $sqlObj;
		$conntion = $sqlObj->mysqlClass();
		$query = $this->setQuery($query,$noreplace);
		$this->_result = mysqli_query($conntion,$query) or $this->setError($query, 'Error : '.mysqli_error($conntion).' : '.$query);
		return mysqli_insert_id($conntion);
	}
	
	function uQuery($query,$noreplace=false)
	{
		global $sqlObj;
		$conntion = $sqlObj->mysqlClass();
		$query = $this->setQuery($query,$noreplace);
		$this->_result = mysqli_query($conntion,$query) or $this->setError($query, 'Error : '.mysqli_error($conntion).' : '.$query);
		return mysqli_affected_rows($conntion);
	}
	function loadResult($query)
	{
		$val = [];
		$query = trim($query);
		if(strtoupper(substr($query, 0, 6)) != 'SELECT')
			$this->setError($query, 'Error : Invalid entry found in loadResult function : '.$query);
		 $this->query($query);
		 $result = $this->_result;
		while($row = mysqli_fetch_assoc($result)){
			$val[] = $row;	
		}
		return $val;
	}

	function fetchNumRows($query)
	{
		$this->query($query);

		return mysqli_num_rows($this->_result);
	}

	function getObjectResult($query)
	{
		$query = trim($query);
		if(strtoupper(substr($query, 0, 6)) != 'SELECT')
			$this->setError($query, 'Error : Invalid entry found in getObjectResult function : '.$query);
		$this->query($query);
		$resultSet = array();
		$result = $this->_result;
			// print_r($result);
		// die;
		while($row = mysqli_fetch_assoc($result)){ 	
		
			$resultSet[] = $row;
        }
        
		return $resultSet;
	}

	function loadAssocList($query)
	{
		global $sqlObj;
		$conntion = $sqlObj->mysqlClass();
		$query = trim($query);
		if(strtoupper(substr($query, 0, 6)) != 'SELECT')
			$this->setError($query, 'Error : Invalid entry found in loadAssocList function : '.$query);

		$this->query($query);

		$resultSet = array();
		$result = $this->_result;
		while($row = mysqli_fetch_assoc($result)){
			$resultSet[] = $row;	
		}

		return $resultSet;
	}

	function loadRow($query)
	{
		$query = trim($query);
		if(strtoupper(substr($query, 0, 6)) != 'SELECT')
			$this->setError($query, 'Error : Invalid entry found in loadRow function : '.$query);

		$this->query($query);
		return mysqli_fetch_row($this->_result);
	}

	function getError()
	{
		if(!empty($this->_error))
			return $this->_error;
	}
	
	function setError($query, $errDesc)
    {
  		echo $errDesc;
 	}


};
?>
