<?php 
/******************************************************************************************************
		File Name: 		Database.class.php
		Owner:			Dhanesh
		Date:			20-01-2009
				
		MODIFICATION HISTORY
		MODIFICATION HISTORY
		_____________________________________________________________________
		| NAME				|	AUTHER		|	MOD.DATE	|	APPROVER 	|
		_____________________________________________________________________
		
		| 					|				|				|			 	|
		
		TABLES: 
		COMPONENTS: 
		PARAMETERS:Nil
		EXTERNAL IMPACT:Nil

	******************************************************************************************************/

#	Description	:	Database class



class Database
{
	var 		$DbLink; 
	var 		$HostName;
	var			$UserName;
	var			$Password;
	var			$DatabaseName;
	var			$ErrorInfo;
		 		
	function Database()
	{
		$this->HostName			=	DB_HOST;
		$this->UserName			=	DB_USER;
		$this->Password			=	DB_PASSWORD;
		$this->DatabaseName		=	DB_NAME;
		$this->dbConnect();
		
	}
				

	#	The following method establish a connection with the database server and on success return TRUE, on failure return FALSE
	#	On failure ErrorInfo property contains the error information.
	/*function dbConnect()
	{
		$this->DbLink 	= 	@mysql_connect($this->HostName, $this->UserName, $this->Password);
		if(!$this->DbLink) { 
			$this->ErrorInfo	=	mysql_error();
			return FALSE;
		} else {
			if(@mysql_select_db($this->DatabaseName, $this->DbLink)) {
				return TRUE;
			} else {
				$this->ErrorInfo	=	mysql_error();
				return FALSE;
			}
		}
	}*/ # Close method dbConnect
		
	
		
	# The following method closes database connection
function dbClose()
	{
		@mysql_close($this->DbLink);
	}
		
	
	
	# On insert, update it returns TRUE,  and on select it returns result set object
	function setQuery($Query)
	{
		$DbLink1 	= 	@mysql_connect($this->HostName, $this->UserName, $this->Password);
		if(@mysql_select_db($this->DatabaseName,$DbLink1)) {
				
			} else {
				$this->ErrorInfo	=	mysql_error();
				echo $this->ErrorInfo;
			exit;
				
			}
		
		$ExecStatus		=	@mysql_query($Query,$DbLink1);
		if($ExecStatus	===	FALSE) {
		
			$this->ErrorInfo	=	mysql_error();
			echo $this->ErrorInfo;
		exit;
			return FALSE;
		} else {
			return $ExecStatus;
		} 
	} # Close method setQuery			
		
	
	# The following method returns a row corresponding to a query
	function readValue($Query)
	{
		$DbLink1 	= 	@mysql_connect($this->HostName, $this->UserName, $this->Password);
		if(@mysql_select_db($this->DatabaseName,$DbLink1)) {
				
			} else {
				$this->ErrorInfo	=	mysql_error();
				exit;
			}
		$Row	=	array();
		$Rs		=	@mysql_query($Query, $DbLink1);	
		
		if(!$Rs) {
			$this->ErrorInfo	=	mysql_error();
			echo $this->ErrorInfo;
			exit;
			return $Row;	 
		} else {
			$Row	=	@mysql_fetch_array($Rs);
			@mysql_free_result($Rs);
			//print_r($Row);//exit;
			return $Row;  		
		}
	}
	
	
	# The following method returns a set of rows as database query result
	function readValues($Query)
	{	
	//echo "hi";exit;
		$DbLink1 	= 	@mysql_connect($this->HostName, $this->UserName, $this->Password);
		if(@mysql_select_db($this->DatabaseName,$DbLink1)) {
				
			} else {
				$this->ErrorInfo	=	mysql_error();
				echo $this->ErrorInfo;
			exit;
				
			}
		$Rows	=	array();
		$Rs		=	@mysql_query($Query,$DbLink1);	
		//echo $Rs;exit;
		if(!$Rs) {
			$this->ErrorInfo	=	mysql_error();
			echo $this->ErrorInfo;
			exit;
			
			return $Rows;	 		
		} else {
			$ArrInx		=	0;
			while($Row	=	@mysql_fetch_array($Rs)) {
				$Rows[$ArrInx]	=	$Row;	
				$ArrInx++;
				//print_r($Row);
			}
			//print_r($Rows);exit;
			//exit;
			//echo $ArrInx;exit;
		}
		//echo "hi";exit;
		//echo $Rows;exit;
		mysql_free_result($Rs);
		return $Rows;
	} # Close method definition
	
	function readValuesSpecial($Query)
	{	
	//echo "hi";exit;
		$DbLink1 	= 	@mysql_connect($this->HostName, $this->UserName, $this->Password);
		if(@mysql_select_db($this->DatabaseName,$DbLink1)) {
				
			} else {
				$this->ErrorInfo	=	mysql_error();
				echo $this->ErrorInfo;
			exit;
				
			}
		$Rows	=	array();
		$Rs		=	@mysql_query($Query,$DbLink1);	
		//echo $Rs;exit;
		if(!$Rs) {
			$this->ErrorInfo	=	mysql_error();
			echo $this->ErrorInfo;
			exit;
			
			return $Rows;	 		
		} else {
			$ArrInx		=	0;
			while($Row	=	@mysql_fetch_assoc($Rs)) {
				$Rows[$ArrInx]	=	$Row;	
				$ArrInx++;
				//print_r($Row);
			}
			//print_r($Rows);exit;
			//exit;
			//echo $ArrInx;exit;
		}
		//echo "hi";exit;
		//echo $Rows;exit;
		mysql_free_result($Rs);
		return $Rows;
	}

        function readValuesSpecialnew($Query)
	{	
	//echo $Query;exit;
		$DbLink1 	= 	@mysql_connect($this->HostName, $this->UserName, $this->Password);
		if(@mysql_select_db($this->DatabaseName,$DbLink1)) {
				
			} else {
				$this->ErrorInfo	=	mysql_error();
				echo $this->ErrorInfo;
			exit;
				
			}
		$Rows	=	array();
		$Rs		=	@mysql_query($Query,$DbLink1);	
		//echo $Rs;exit;
		if(!$Rs) {
			$this->ErrorInfo	=	mysql_error();
			echo $this->ErrorInfo;
			exit;
			
			return $Rows;	 		
		} else {
			$ArrInx		=	0;
			while($Row	=	@mysql_fetch_assoc($Rs)) {
				$Rows[$ArrInx]	=	$Row;	
				$ArrInx++;
				print_r($Row);
			}
			//print_r($Rows);exit;
			//exit;
			echo $ArrInx;exit;
		}
		//echo "hi";exit;
		//echo $Rows;
                exit;
		mysql_free_result($Rs);
		return $Rows;
	}
	
	
		
	# The following method return the affected rows during database operation
	function getAffectedRows()
	{
		$AffectedRows	=	@mysql_affected_rows($this->DbLink);
		return $AffectedRows;
	}
		
		
	# Method returns the last insert Id of this connection	
	function getInsertId()
	{
		$DbLink1 	= 	@mysql_connect($this->HostName, $this->UserName, $this->Password);
	if(@mysql_select_db($this->DatabaseName,$DbLink1)) {
				
			} else {
				$this->ErrorInfo	=	mysql_error();
				echo $this->ErrorInfo;
			exit;
				
			}
		$InsertId	=	@mysql_insert_id($DbLink1);
		return $InsertId; 		
	}
	#Method returns the fieldname 
	function getFieldName($Query,$i)
	{
	$Qry = mysql_field_name($Query,$i);
	return $Qry;
	
	}
	
	#returns the number of fields in the result set
	function getNumFields($Query)
	{
	$SqlQry = @mysql_num_fields($Query);
	return $SqlQry;
	
	}
	#method returns the number of records in the resultset
	function numberOfRecords($Query)
	{
		$RowCount	=	0;
		$rs=mysql_query($Query);
		$RowCount	=	mysql_num_rows($rs);
		
			return $RowCount;
	
	}
	#fetch a row of the resultset
	function fetchRows($Query)
	{
	$fetchRow = @mysql_fetch_row($Query);
	return $fetchRow;
	}
	#fetches all the resultset into an array
	function fetchArray($Query)
	{
	$fetchRow = @mysql_fetch_array($Query);
	return $fetchRow;
	}
	#execute the query, returns a result id if true
	function MysqlQry($Query)
	{
	$QryReturn = @mysql_query($Query);
	
	return $QryReturn;
	
	}
	function RecordCount($rs)
		{
			if($rs && @mysql_num_rows($rs)>0)
				$result	=	 @mysql_num_rows($rs);
			else 
				$result	=	0;
			return $result;
		}
	
	
} # Close class definition
?>
