1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * Database Result Class
21 * This is the platform-independent result class.
22 * This class will not be called directly. Rather, the adapter
23 * class for the specific database will extend and instantiate it.
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/database/
32 var $result_id = NULL;
33 var $result_array = array();
34 var $result_object = array();
35 var $custom_result_object = array();
42 * Query result. Acts as a wrapper function for the following functions.
45 * @param string can be "object" or "array"
46 * @return mixed either a result object or array
48 function result($type = 'object')
50 if ($type == 'array') return $this->result_array();
51 else if ($type == 'object') return $this->result_object();
52 else return $this->custom_result_object($type);
55 // --------------------------------------------------------------------
58 * Custom query result.
60 * @param class_name A string that represents the type of object you want back
61 * @return array of objects
63 function custom_result_object($class_name)
65 if (array_key_exists($class_name, $this->custom_result_object))
67 return $this->custom_result_object[$class_name];
70 if ($this->result_id === FALSE OR $this->num_rows() == 0)
75 // add the data to the object
77 $result_object = array();
78 while ($row = $this->_fetch_object())
80 $object = new $class_name();
81 foreach ($row as $key => $value)
83 $object->$key = $value;
85 $result_object[] = $object;
89 return $this->custom_result_object[$class_name] = $result_object;
93 * Query result. "object" version.
98 function result_object()
100 if (count($this->result_object) > 0)
102 return $this->result_object;
105 // In the event that query caching is on the result_id variable
106 // will return FALSE since there isn't a valid SQL resource so
107 // we'll simply return an empty array.
108 if ($this->result_id === FALSE OR $this->num_rows() == 0)
113 $this->_data_seek(0);
114 while ($row = $this->_fetch_object())
116 $this->result_object[] = $row;
119 return $this->result_object;
122 // --------------------------------------------------------------------
125 * Query result. "array" version.
130 function result_array()
132 if (count($this->result_array) > 0)
134 return $this->result_array;
137 // In the event that query caching is on the result_id variable
138 // will return FALSE since there isn't a valid SQL resource so
139 // we'll simply return an empty array.
140 if ($this->result_id === FALSE OR $this->num_rows() == 0)
145 $this->_data_seek(0);
146 while ($row = $this->_fetch_assoc())
148 $this->result_array[] = $row;
151 return $this->result_array;
154 // --------------------------------------------------------------------
157 * Query result. Acts as a wrapper function for the following functions.
161 * @param string can be "object" or "array"
162 * @return mixed either a result object or array
164 function row($n = 0, $type = 'object')
166 if ( ! is_numeric($n))
168 // We cache the row data for subsequent uses
169 if ( ! is_array($this->row_data))
171 $this->row_data = $this->row_array(0);
174 // array_key_exists() instead of isset() to allow for MySQL NULL values
175 if (array_key_exists($n, $this->row_data))
177 return $this->row_data[$n];
179 // reset the $n variable if the result was not achieved
183 if ($type == 'object') return $this->row_object($n);
184 else if ($type == 'array') return $this->row_array($n);
185 else return $this->custom_row_object($n, $type);
188 // --------------------------------------------------------------------
191 * Assigns an item into a particular column slot
196 function set_row($key, $value = NULL)
198 // We cache the row data for subsequent uses
199 if ( ! is_array($this->row_data))
201 $this->row_data = $this->row_array(0);
206 foreach ($key as $k => $v)
208 $this->row_data[$k] = $v;
214 if ($key != '' AND ! is_null($value))
216 $this->row_data[$key] = $value;
220 // --------------------------------------------------------------------
223 * Returns a single result row - custom object version
228 function custom_row_object($n, $type)
230 $result = $this->custom_result_object($type);
232 if (count($result) == 0)
237 if ($n != $this->current_row AND isset($result[$n]))
239 $this->current_row = $n;
242 return $result[$this->current_row];
246 * Returns a single result row - object version
251 function row_object($n = 0)
253 $result = $this->result_object();
255 if (count($result) == 0)
260 if ($n != $this->current_row AND isset($result[$n]))
262 $this->current_row = $n;
265 return $result[$this->current_row];
268 // --------------------------------------------------------------------
271 * Returns a single result row - array version
276 function row_array($n = 0)
278 $result = $this->result_array();
280 if (count($result) == 0)
285 if ($n != $this->current_row AND isset($result[$n]))
287 $this->current_row = $n;
290 return $result[$this->current_row];
294 // --------------------------------------------------------------------
297 * Returns the "first" row
302 function first_row($type = 'object')
304 $result = $this->result($type);
306 if (count($result) == 0)
313 // --------------------------------------------------------------------
316 * Returns the "last" row
321 function last_row($type = 'object')
323 $result = $this->result($type);
325 if (count($result) == 0)
329 return $result[count($result) -1];
332 // --------------------------------------------------------------------
335 * Returns the "next" row
340 function next_row($type = 'object')
342 $result = $this->result($type);
344 if (count($result) == 0)
349 if (isset($result[$this->current_row + 1]))
351 ++$this->current_row;
354 return $result[$this->current_row];
357 // --------------------------------------------------------------------
360 * Returns the "previous" row
365 function previous_row($type = 'object')
367 $result = $this->result($type);
369 if (count($result) == 0)
374 if (isset($result[$this->current_row - 1]))
376 --$this->current_row;
378 return $result[$this->current_row];
381 // --------------------------------------------------------------------
384 * The following functions are normally overloaded by the identically named
385 * methods in the platform-specific driver -- except when query caching
386 * is used. When caching is enabled we do not load the other driver.
387 * These functions are primarily here to prevent undefined function errors
388 * when a cached result object is in use. They are not otherwise fully
389 * operational due to the unavailability of the database resource IDs with
392 function num_rows() { return $this->num_rows; }
393 function num_fields() { return 0; }
394 function list_fields() { return array(); }
395 function field_data() { return array(); }
396 function free_result() { return TRUE; }
397 function _data_seek() { return TRUE; }
398 function _fetch_assoc() { return array(); }
399 function _fetch_object() { return array(); }
402 // END DB_result class
404 /* End of file DB_result.php */
405 /* Location: ./system/database/DB_result.php */