<?php

class mysqlClass

{

	var $_conn = ''; //Stores the connection string

	var $_error = '';

	var $_result = '';

	function mysqlClass()

	{

		global $dbhost, $dbuser, $dbpassword, $dbname, $dbprefix;



		$this->_conn = mysql_connect($dbhost, $dbuser, $dbpassword);

		$result = mysql_select_db($dbname, $this->_conn);

	}



	function setQuery($query,$noreplace=false)

	{

		$query =  trim($query);

		global $dbprefix;

		if(!$noreplace)

		{

			$query = str_replace('##', $dbprefix, $query);

		}

		return $query;

	}



	function query($query,$noreplace=false)

	{

		$query = $this->setQuery($query,$noreplace);

		$this->_result = mysql_query($query) or $this->setError($query, 'Error : '.mysql_error().' : '.$query);

		if(strtoupper(substr($query, 0, 6)) == 'INSERT')

		{

			return mysql_insert_id();

		}	

	}

	

	function iQuery($query,$noreplace=false)

	{

		$query = $this->setQuery($query,$noreplace);

		$this->_result = mysql_query($query) or $this->setError($query, 'Error : '.mysql_error().' : '.$query);

		return mysql_insert_id();

	}

	

	function uQuery($query,$noreplace=false)

	{

		$query = $this->setQuery($query,$noreplace);

		$this->_result = mysql_query($query) or $this->setError($query, 'Error : '.mysql_error().' : '.$query);

		return mysql_affected_rows();

	}



	function loadResult($query)

	{

		$query = trim($query);

		if(strtoupper(substr($query, 0, 6)) != 'SELECT')

			$this->setError($query, 'Error : Invalid entry found in loadResult function : '.$query);



		 $this->query($query);

		

		

		if(empty($this->_error))

		{

			$val = @mysql_result($this->_result, 0);

			}



		return $val;

	}



	function fetchNumRows($query)

	{

		$this->query($query);



		return mysql_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();

		



		while($row = mysql_fetch_object($this->_result))

			$resultSet[] = $row;		

		return $resultSet;

	}



	function loadAssocList($query)

	{

		$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();



		while($row = mysql_fetch_array($this->_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 mysql_fetch_row($this->_result);

	}



	function getError()

	{

		if(!empty($this->_error))

			return $this->_error;

	}

	

	function setError($query, $errDesc)

    {

  		echo $errDesc;

 	}





};

?>

