Location: PHPKode > scripts > sqltoolkit > sqltoolkit/database_result.php
<?php
/*

    database_result.php - A PHP class for storing database query results
    Copyright (C) 2003 Erik Giberti (AF-Design), All rights reserved.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

if(!defined("_DATABASE_RESULT_CLASS_")) { 
	define("_DATABASE_RESULT_CLASS_", TRUE); 

	class database_result {
		var $result_data;   // holds the array of data
		var $result_info;   // holds an array for keeping tabs on size, position etc		

		/* 
	
			CONSTRUCTOR 
	
		*/

		function database_result($result_resource = "", $database_type = "EXTERNAL"){
			/* 
				LOCAL FUNCTIONS 
			*/
			if(!defined("_DATABASE_RESULT_CLASS_FUNCTIONS_")){
			define("_DATABASE_RESULT_CLASS_FUNCTIONS_", TRUE);		
			// to parse out the info on row count etc
			function database_data_info($database_type, $result_resource){
				switch (strtoupper($database_type)){
					case "MYSQL":
						$info["num_rows"] = @mysql_num_rows($result_resource) or
						$info["num_rows"] = @mysql_affected_rows($result_resource) or
						$info["num_rows"] = 0;
						$info["current_row"] = 0;
						$info["num_fields"]  = @mysql_num_fields($result_resource) or
						$info["num_fields"]  = 0;
						break;
					case "MSSQL":
						$info["num_rows"]    = @mssql_num_rows($result_resource);
						$info["current_row"] = 0;
						$info["num_fields"]  = @mssql_num_fields($result_resource);				
						break;
					case "POSTGRES":
					case "POSTGRE95":
					case "POSTGRESQL":
						$info["num_rows"] = @pg_num_rows($result_resource) or
						$info["num_rows"] = @pg_affected_rows($result_resource) or
						$info["num_rows"] = 0;
						$info["current_row"] = 0;
						$info["num_fields"]  = @pg_num_fields($result_resource) or
						$info["num_fields"]  = 0;
						break;
					case "MSQL":
						$info["num_rows"] = @msql_num_rows($result_resource) or
						$info["num_rows"] = @msql_affected_rows($result_resource) or
						$info["num_rows"] = 0;
						$info["current_row"] = 0;
						$info["num_fields"]  = @msql_num_fields($result_resource) or
						$info["num_fields"]  = 0;
						break;
					case "ODBC":
						$info["num_rows"] = @odbc_num_rows($result_resource) or
						$info["num_rows"] = 0;
						$info["current_row"] = 0;
						$info["num_fields"]  = @odbc_num_fields($result_resource) or
						$info["num_fields"]  = 0;
						break;
					default:
						$info = FALSE;
						break;
				}
				return $info;
			}			
			// to parse the resource into a 2 dimentional array
			function database_data_normal($database_type, $dataset){
				switch (strtoupper($database_type)){
					case "MYSQL":
						if (@mysql_num_rows($dataset) > 0){
						for ($row=0; $row<mysql_num_rows($dataset); $row++){
							mysql_data_seek($dataset, $row);
							$this_row_assoc = mysql_fetch_assoc($dataset);
							$column_name_array = array_keys($this_row_assoc);
							foreach ($column_name_array as $column){
							$normalizedData[$row][$column] = $this_row_assoc[$column]; 
							}
						}
						} else {
							$normalizedData[0]["affected_rows"] = @mysql_affected_rows($dataset);
						}
						break;
					case "MSSQL":
						for ($row=0; $row<mssql_num_rows($dataset); $row++){
							mssql_data_seek($dataset, $row);
							$this_row_assoc = mssql_fetch_assoc($dataset);
							$column_name_array = array_keys($this_row_assoc);
							foreach ($column_name_array as $column){
							$normalizedData[$row][$column] = $this_row_assoc[$column]; 
							}
						}
						break;
					case "POSTGRES":
					case "POSTGRES95":
					case "POSTGRESQL":
						for ($row=0; $row<pg_num_rows($dataset); $row++){
							$this_row_assoc = pg_fetch_assoc($dataset, $row);
							$column_name_array = array_keys($this_row_assoc);
							foreach ($column_name_array as $column){
							$normalizedData[$row][$column] = $this_row_assoc[$column]; 
							}
						}
						break;
					case "MSQL":
						for ($row=0; $row<msql_num_rows($dataset); $row++){
							msql_data_seek($dataset, $row);
							$this_row_object = msql_fetch_object($dataset);
							$column_name_array = array_keys($this_row_assoc);
							foreach ($column_name_array as $column){
							$normalizedData[$row][$column] = $this_row_object->$column; 
							}
						}
						break;
					case "ODBC":
						for ($row=0; $row<odbc_num_rows($dataset); $row++){
							$this_row_object = odbc_fetch_object($dataset, $row);
							$column_name_array = array_keys($this_row_assoc);
							foreach ($column_name_array as $column){
							$normalizedData[$row][$column] = $this_row_object->$column; 
							}
						}
						break;
					default:
						$normalizedData = FALSE;
						break;	
				}
				return $normalizedData;
			}
			function database_data_free_result($dbtype, $resource){
				switch (strtoupper($database_type)){
					case "MYSQL":
						$return_var = mysql_free_result($resource);
						break;
					case "MSSQL":
						$return_var = mssql_free_result($resource);
						break;
					case "POSTGRES":
					case "POSTGRES95":
					case "POSTGRESQL":
						$return_var = pg_free_result($resource);
						break;
					case "MSQL":
						$return_var = msql_free_result($resource);
					case "ODBC":
						$return_var = odbc_free_result($resource);
					default:
						$return_var = FALSE;
						break;
				}
				return $return_var;
			}
			} // end function lock
			if ($database_type == "EXTERNAL"){
				// create an empty container to populate later
				$this->result_data = array();
				$this->result_info = array();
			} else {
				$this->result_data = database_data_normal($database_type, $result_resource);
				$this->result_info = database_data_info($database_type, $result_resource);
				database_data_free_result($database_type, $result_resource);
			}
		}	

		/*
	
			CLASS METHODS 
	
		*/

		// utility functions
		// array fetch_data(void);
		function fetch_data(){
			return $this->result_data;
		}
		// array fetch_info(void);
		function fetch_info(){
			return $this->result_info;
		}
		
		// return a row
		// array fetch_assoc(void);
		function fetch_assoc(){
			if($this->result_info["current_row"] < $this->result_info["num_rows"] &&
			   $this->result_info["num_fields"] > 0) {
			   $return_array = $this->result_data[$this->result_info["current_row"]];
			   $this->result_info["current_row"]++;
			   return $return_array;
			} else {
			   return false;
			}
		}
		// array fetch_row(void);
		function fetch_row(){
			if($this->result_info["current_row"] < $this->result_info["num_rows"] &&
			   $this->result_info["num_fields"] > 0) {
			   $field_names = array_keys($this->result_data[$this->result_info["current_row"]]);
			   $row_values  = $this->result_data[$this->result_info["current_row"]];
			   $column      = 0;
			   foreach($field_names AS $field){
			   		$return_array[$column] = $row_values[$field];
			   		$column++;
			   }
			   $this->result_info["current_row"]++;
			   return $return_array;
			} else {
			   return false;
			}
		}
		// object fetch_object(void);
		function fetch_object(){
			if($this->result_info["current_row"] < $this->result_info["num_rows"] &&
			   $this->result_info["num_fields"] > 0) {
			    $field_names = array_keys($this->result_data[$this->result_info["current_row"]]);
			   	$row_values  = $this->result_data[$this->result_info["current_row"]];
			    foreach($field_names AS $field){
			    	$return_object->$field = $row_values[$field];
			    }
			    return $return_object;
			} else {
				return false;
			}
		}

		// return info
		// int num_rows(void);
		function num_rows(){
			return $this->result_info["num_rows"];
		}
		// int num_fields(void);
		function num_fields(){
			return $this->result_info["num_fields"];
		}
		
		// manipulate pointers etc
		// bool data_seek(int);
		function data_seek($rownum){
			if ($this->result_info["num_rows"] > $rownum &&
			    $rownum >= 0){
				$this->result_info["current_row"] = $rownum;
				return TRUE;
			} else {
				return FALSE;
			}
			
		}
		
		// for future expandability these should never be called directly
		// only through some sort of data parser such as an XML parser		
		function assign_external_data($data){
			$this->result_data = $data;
		}
		function assign_external_info($info){
			$this->result_info = $info;
		}
		

	} // end class database_result()
} // end defined("_DATABASE_RESULT_CLASS_")


/*

	END OF FILE
	
*/
?>
Return current item: sqltoolkit