Location: PHPKode > scripts > rhc class > rhc-class/rhc_db_object.php
<?php
/*
DB_OBJECT: a php class which handles connecting to a database, querying the db, and fetching results, and freeing results
AUTHOR: Rick Hopkins
DATE: October 3, 2002
UPDATED LAST: October 16, 2002
*/

//include rh_baseclass.php for debugging
require_once("rhc_baseclass.php");

/********************************************************************************************************************************/
class db_object extends baseclass
{//class in-session
	//declare variables
	var $db_status;
	var $db_type;
	var $db_string;
	var $db_user;
	var $db_passwd;
	
	/********************************************************************/
	//function db_object() will setup variables and initiate baseclass
	function db_object()
	{
		//initiate baseclass()
		baseclass::baseclass();
		
		//setup variable values => if ever needed to change, change here  // $this->db_type in all caps
		$this->db_type = "DBTYPE";
		$this->db_string = "dsn";
		$this->db_user = "username";
		$this->db_passwd = "password";
		$this->db_status = 0;
	}
	
	/********************************************************************/
	//function db_connect() will create the connection to db (currently under informix commands)
	function db_connect()
	{
		$this->write_debug_log("Attempting To Open ".$this->db_type." Database Connection", "GREEN");
		switch ($this->db_type)
		{
			case "INFORMIX":
				$conn = ifx_connect($this->db_string, $this->db_user, $this->db_passwd);
				break;
			case "ODBC":
				$conn = odbc_connect($this->db_string, $this->db_user, $this->db_passwd);
				break;
		}
		
		if ($conn){
			$this->write_debug_log($this->db_type." Database Connection Made Successfully", "GREEN");
			$this->set_db_status(1);
			}else{
			$this->write_debug_log($this->db_type." Database Connection Failure", "RED");
			}
		
		return $conn;
	}
	
	/********************************************************************/
	//function db_exec() will perform a query on the database
	function db_exec($sql_string, $conn)
	{
		$this->write_debug_log("Attempting to Perform ".$this->db_type." Query: \"$sql_string\"", "GREEN");
		switch ($this->db_type)
		{
			case "INFORMIX":
				$result = ifx_query($sql_string, $conn);
				break;
			case "ODBC":
				$result = odbc_exec($conn, $sql_string);
				break;
		}
		
		if ($result){
			$this->write_debug_log($this->db_type." Query Performed Successfully", "GREEN");
			}else{
			$this->write_debug_log($this->db_type." Query Performance Failure", "RED");
			}
		
		return $result;
	}
	
	/********************************************************************/
	//function db_fetch() will retrieve rows of data from the result created in db_exec
	function db_fetch($result)
	{
		switch ($this->db_type)
		{
			case "INFORMIX";
				$row = ifx_fetch_row($result, "NEXT");
				break;
			case "ODBC";
				$num_fields = odbc_num_fields($result);
				if (odbc_fetch_row($result)){
					for($i=1;$i<=$num_fields;$i++){
						$field_name=odbc_field_name($result,$i);
						$field_value=odbc_result($result,$i);
						$row[$field_name]=$field_value;
						}
					}
				break;
		}
		
		if (!($row)){
			$this->write_debug_log($this->db_type." Row Fetch Attempt Failure", "RED");
			}else{
			$this->write_debug_log("Attempting To Fetch Row From ".$this->db_type." Database Result", "GREEN");
			}
		return $row;
	}
	
	/********************************************************************/
	//function db_free() will free the result gotten from db_exec()
	function db_free($result)
	{
		$this->write_debug_log("Releasing ".$this->db_type." Database Query Result", "GREEN");
		switch ($this->db_type)
		{
			case "INFORMIX":
				ifx_free_result($result);
				break;
			case "ODBC":
				odbc_free_result($result);
				break;
		}
		
	}
	
	/********************************************************************/
	//function db_close() will close the connection to the db
	function db_close($conn)
	{
		$this->write_debug_log("Closing ".$this->db_type." Database Connection", "GREEN");
		switch ($this->db_type)
		{
			case "INFORMIX":
				ifx_close($conn);
				break;
			case "ODBC":
				odbc_close($conn);
				break;
		}
		
		$this->set_db_status(0);
	}
	
	/********************************************************************/
	//function set_db_status() will change the status of the db connection depending on connection or not
	function set_db_status($status)
	{
		if ($status == 1){
			$this->db_status = 1;
			}else{
			$this->db_status = 0;
			}
	}
	
	/********************************************************************/
	//function get_db_status() will return the status of the db connection
	function get_db_status()
	{
		if ($this->db_status > 0){
			$this->write_debug_log("DB_CONN_STATUS: ".$this->db_type." CONNECTION GOOD", "GREEN");
			return 1;
			}else{
			$this->write_debug_log("DB_CONN_STATUS: NO CONNECTION TO ".$this->db_type, "RED");
			return 0;
			}
	}
	
	/********************************************************************/
}//class out
/********************************************************************************************************************************/
?>
Return current item: rhc class