Location: PHPKode > projects > FxFramework > fxFrm/fxDataBase.php
<?php

include_once("3rd_party_lib/adodb5/adodb.inc.php");
include_once('fxErrors.php');

class FxDataBase
{
	private $_host;
	private $_user;
	private $_password;
	private $_dataBase;
	private $_driver;
	
	public function FxDataBase($host, $user, $password, $dataBase, $driver)
	{
		$this->_host = $host;
		$this->_user = $user;
		$this->_password = $password;
		$this->_dataBase = $dataBase;	
		$this->_driver = $driver;
	}
	
	public function fxDbQuery($query, &$recordSet = "")
	{
		//First Connect
		$dsn = $this->_driver.'://'.$this->_user.':'.$this->_password.'@'.$this->_host.'/'.$this->_dataBase;
		@$db = NewADOConnection($dsn);
		
		//If no connection is stablished return false
		if (!$db)
		{
			$dsn = str_replace($this->_password, "*****", $dsn);
			
			$msg = "Cannot stablish connection to database.<br><b>Connection dsn was: </b>".$dsn;
			$msg .= "<br><br><i><b>NOTICE: </b>Password is hidden for security reasons. If all paramaters are correct 
			the most likely error would be an incorrect passord or that the server is down.</i>";
			
			FxError::fxCritical($msg);
			return false;
		}
		
		$db->SetFetchMode(ADODB_FETCH_ASSOC); 
		
		$rSet = &$db->Execute($query);
		
		if ($rSet === false)
		{
			$msg = "<b>Error returner by server was: </b>".$db->ErrorMsg();
			$msg .= "<br><br><b>Query was: </b><pre>".$query."</pre>";
			$db->Close();
			FxError::fxCritical($msg);
			return false;
		}
		
		if(is_a($rSet, "ADORecordSet"))
		{
			$recordSet = array();
			
			while(!$rSet->EOF)
			{
				$recordSet[] = $rSet->fields;
				$rSet->MoveNext();
			}
		}
		$db->Close();
		
		return true;
	}
	
	public function fxExecuteTransaction($sqlArray)
	{
		if(!is_array($sqlArray))
		{
			FxError::fxCritical("Argument suplied for fxExecuteTrans is not an Array.");
		}
		
		//First Connect
		$dsn = $this->_driver.'://'.$this->_user.':'.$this->_password.'@'.$this->_host.'/'.$this->_dataBase;
		@$db = NewADOConnection($dsn);
		
		//If no connection is stablished return false
		if (!$db)
		{
			$dsn = str_replace($this->_password, "*****", $dsn);
			
			$msg = "Cannot stablish connection to database.<br><b>Connection dsn was: </b>".$dsn;
			$msg .= "<br><br><i><b>NOTICE: </b>Password is hidden for security reasons. If all paramaters are correct 
			the most likely error would be an incorrect passord or that the server is down.</i>";
			
			FxError::fxCritical($msg);
			return false;
		}
		
		$db->BeginTrans();
		foreach($sqlArray as $query)
		{
			if ($db->Execute($query) === false)
			{
				$dbError = $db->ErrorMsg();
				$db->RollbackTrans();
				$db->Close();
				$msg = "<b>Error returner by server was: </b>".$dbError;
				$msg .= "<br><br><b>Query was: </b><pre>".$query."</pre>";
				FxError::fxCritical($msg);
				return false;
			}
		}
		$db->CommitTrans();
		
		$db->Close();
		
		return true;
	}
	
	public function fxDbGetRowCount($query)
	{
		//First Connect
		$dsn = $this->_driver.'://'.$this->_user.':'.$this->_password.'@'.$this->_host.'/'.$this->_dataBase;
		@$db = NewADOConnection($dsn);
		
		//If no connection is stablished return false
		if (!$db)
		{
			$dsn = str_replace($this->_password, "*****", $dsn);
			
			$msg = "Cannot stablish connection to database.<br><b>Connection dsn was: </b>".$dsn;
			$msg .= "<br><br><i><b>NOTICE: </b>Password is hidden for security reasons. If all paramaters are correct 
			the most likely error would be an incorrect passord or that the server is down.</i>";
			
			FxError::fxCritical($msg);
			return -1;
		}
		
		$db->SetFetchMode(ADODB_FETCH_ASSOC); 
		
		$rSet = &$db->Execute($query);
		
		if ($rSet === false)
		{
			$msg = "<b>Error returner by server was: </b>".$db->ErrorMsg();
			$msg .= "<br><br><b>Query was: </b><pre>".$query."</pre>";
			$db->Close();
			FxError::fxCritical($msg);
			return -1;
		}
		
		if(is_a($rSet, "ADORecordSet"))
		{
			$rowCount = $rSet->RecordCount();
		}
		$db->Close();
		
		return $rowCount;
	}
	
	public function fxDbExists($table, $condition)
	{
		$sql = "select 1 from ".$table." where ".$condition;
		return ($this->fxDbGetRowCount($sql) > 0) ? true : false;
	}
	
}

?>
Return current item: FxFramework