Location: PHPKode > scripts > Database Queries > database-queries/DatabaseQueries.php
<?php

/**
* class DatabaseQueries
*
* { Description :- 
*	This class is the base class for executing database queries
* }
*/

class DatabaseQueries
{
	var $conHandle;
	var $query;
	var $result;
	
	/**
	* Method DatabaseQueries::getResultSet()
	*
	* { Description :- 
	*	This class is the base class for executing database queries.
	* }
	*/
	
	function getResultSet()
	{
		return $this->result;
	}
}


/**
* class MySqlDatabaseQueries
*
* { Description :- 
*	This class is the sub class for executing MySql database queries.
* }
*/

class MySqlDatabaseQueries extends DatabaseQueries
{	
	
	function MySqlDatabaseQueries($query, $conHandle)
	{
		$this->query = $query;
		$this->conHandle = $conHandle;
	}
	
	/**
	* Method MySqlDatabaseQueries::executeQuery()
	*
	* { Description :- 
	*	This method executes the the query.
	* }
	*/
	
	function executeQuery()
	{
		if(!($this->result = mysql_query($this->query, $this->conHandle)))
		{
			return false;
		}
		else
		{
			return true;	
		}
	}
	
	/**
	* Method MySqlDatabaseQueries::getNumRows()
	*
	* { Description :- 
	*	This method returns the total rows in the resultset.
	* }
	*/
	
	function getNumRows()
	{
		if(is_resource($this->result))
		{
			return mysql_num_rows($this->result);
		}
		else
		{
			return -1;	
		}
	}
	
	/**
	* Method MySqlDatabaseQueries::getResultArray()
	*
	* { Description :- 
	*	This method returns the one row from the resultset
	* }
	*/
	
	function getResultArray()
	{
		if(is_resource($this->result))
		{
			return mysql_fetch_array($this->result);
		}
		else
		{
			return null;
		}
	}
}

/**
* class MSSqlDatabaseQueries
*
* { Description :- 
*	This class is the sub class for executing MSSql database queries.
* }
*/

class MSSqlDatabaseQueries extends DatabaseQueries
{	
	
	/**
	* Method MSSqlDatabaseQueries::executeQuery()
	*
	* { Description :- 
	*	This method executes the the query.
	* }
	*/
	
	function MSSqlDatabaseQueries($query, $conHandle)
	{
		$this->query = $query;
		$this->conHandle = $conHandle;
	}
	
	/**
	* Method MSSqlDatabaseQueries::executeQuery()
	*
	* { Description :- 
	*	This method executes the the query.
	* }
	*/
	
	function executeQuery()
	{
		if(!($this->result = mssql_query($this->query, $this->conHandle)))
		{
			return false;
		}
		else
		{
			return true;	
		}
	}
	
	/**
	* Method MSSqlDatabaseQueries::getNumRows()
	*
	* { Description :- 
	*	This method returns the total rows in the resultset.
	* }
	*/
	
	function getNumRows()
	{
		if(is_resource($this->result))
		{
			return mssql_num_rows($this->result);
		}
		else
		{
			return -1;
		}
	}
	
	/**
	* Method MSSqlDatabaseQueries::getResultArray()
	*
	* { Description :- 
	*	This method returns the one row from the resultset
	* }
	*/
	
	function getResultArray()
	{
		if(is_resource($this->result))
		{
			return mssql_fetch_array($this->result);
		}
		else
		{
			return null;
		}
	}
}

/**
* class MSSqlDatabaseQueryProc
*
* { Description :- 
*	This class is the sub of MSSqlDatabaseQuery class for executing MSSql database Procedures.
*	$proc -- Procedure Name.
*	$conHandle -- Connection Handle.
*	$params -- Associative array  eg. array("@edited"=>array($edited=>SQLCHAR, false));
*										@edited is input paramter,
*										$edited is the value of Input Parameter @edited,
*										SQLCHAR is a the MSSQL Constant for CHAR column type,
*										false indicates @edited is not an output parameter.		
*				
* }							
*/

class MSSqlDatabaseQueryProc extends MSSqlDatabaseQueries
{
	var $params;
	var $paramName;
	var $paramValue;
	var $paramType;	
	
	/* Method MSSqlDatabaseQueryProc(). Constructor.
	*
	* { Description :- 
	*	This class is the sub of MSSqlDatabaseQuery class for executing MSSql database Procedures.
	*	$proc -- Procedure Name.
	*	$conHandle -- Connection Handle.
	*	$params -- Associative array  eg. array("@edited"=>array($edited=>SQLCHAR, false));
	*										@edited is input paramter,
	*										$edited is the value of Input Parameter @edited,
	*										SQLCHAR is a the MSSQL Constant for CHAR column type,
	*										false indicates @edited is not an output parameter.		
	*				
	* }							
	*/	
	
	function MSSqlDatabaseQueryProc($proc, $conHandle, $params)
	{
		$this->query = $proc;
		$this->conHandle = $conHandle;
		$this->params = $params;
	}
	
	/**
	* Method MSSqlDatabaseQueries::executeQuery()
	*
	* { Description :- 
	*	This method executes the the query.
	* }
	*/	
	
	function executeQuery()
	{
		$stmt = mssql_init("$this->query",$this->conHandle);
		
		foreach($this->params as $paramName=>$value)
		{
			$booleanInOut = $value[0];
			foreach($value as $paramValue=>$paramType)
			{
				if($booleanInOut)
				{
					mssql_bind($stmt, "$paramName", $paramValue, $paramType, true);
				}
				else
					mssql_bind($stmt, "$paramName", $paramValue, $paramType);
				break;
			}
		}
		
		if(!($this->result = mssql_execute($stmt)))
		{
			return false;
		}
		else
			return true;
	}
} 
?>
Return current item: Database Queries