1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, 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();
41 * Query result. Acts as a wrapper function for the following functions.
44 * @param string can be "object" or "array"
45 * @return mixed either a result object or array
47 function result($type = 'object')
49 return ($type == 'object') ? $this->result_object() : $this->result_array();
52 // --------------------------------------------------------------------
55 * Query result. "object" version.
60 function result_object()
62 if (count($this->result_object) > 0)
64 return $this->result_object;
67 // In the event that query caching is on the result_id variable
68 // will return FALSE since there isn't a valid SQL resource so
69 // we'll simply return an empty array.
70 if ($this->result_id === FALSE OR $this->num_rows() == 0)
76 while ($row = $this->_fetch_object())
78 $this->result_object[] = $row;
81 return $this->result_object;
84 // --------------------------------------------------------------------
87 * Query result. "array" version.
92 function result_array()
94 if (count($this->result_array) > 0)
96 return $this->result_array;
99 // In the event that query caching is on the result_id variable
100 // will return FALSE since there isn't a valid SQL resource so
101 // we'll simply return an empty array.
102 if ($this->result_id === FALSE OR $this->num_rows() == 0)
107 $this->_data_seek(0);
108 while ($row = $this->_fetch_assoc())
110 $this->result_array[] = $row;
113 return $this->result_array;
116 // --------------------------------------------------------------------
119 * Query result. Acts as a wrapper function for the following functions.
123 * @param string can be "object" or "array"
124 * @return mixed either a result object or array
126 function row($n = 0, $type = 'object')
128 if ( ! is_numeric($n))
130 // We cache the row data for subsequent uses
131 if ( ! is_array($this->row_data))
133 $this->row_data = $this->row_array(0);
136 // array_key_exists() instead of isset() to allow for MySQL NULL values
137 if (array_key_exists($n, $this->row_data))
139 return $this->row_data[$n];
141 // reset the $n variable if the result was not achieved
145 return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
148 // --------------------------------------------------------------------
151 * Assigns an item into a particular column slot
156 function set_row($key, $value = NULL)
158 // We cache the row data for subsequent uses
159 if ( ! is_array($this->row_data))
161 $this->row_data = $this->row_array(0);
166 foreach ($key as $k => $v)
168 $this->row_data[$k] = $v;
174 if ($key != '' AND ! is_null($value))
176 $this->row_data[$key] = $value;
180 // --------------------------------------------------------------------
183 * Returns a single result row - object version
188 function row_object($n = 0)
190 $result = $this->result_object();
192 if (count($result) == 0)
197 if ($n != $this->current_row AND isset($result[$n]))
199 $this->current_row = $n;
202 return $result[$this->current_row];
205 // --------------------------------------------------------------------
208 * Returns a single result row - array version
213 function row_array($n = 0)
215 $result = $this->result_array();
217 if (count($result) == 0)
222 if ($n != $this->current_row AND isset($result[$n]))
224 $this->current_row = $n;
227 return $result[$this->current_row];
231 // --------------------------------------------------------------------
234 * Returns the "first" row
239 function first_row($type = 'object')
241 $result = $this->result($type);
243 if (count($result) == 0)
250 // --------------------------------------------------------------------
253 * Returns the "last" row
258 function last_row($type = 'object')
260 $result = $this->result($type);
262 if (count($result) == 0)
266 return $result[count($result) -1];
269 // --------------------------------------------------------------------
272 * Returns the "next" row
277 function next_row($type = 'object')
279 $result = $this->result($type);
281 if (count($result) == 0)
286 if (isset($result[$this->current_row + 1]))
288 ++$this->current_row;
291 return $result[$this->current_row];
294 // --------------------------------------------------------------------
297 * Returns the "previous" row
302 function previous_row($type = 'object')
304 $result = $this->result($type);
306 if (count($result) == 0)
311 if (isset($result[$this->current_row - 1]))
313 --$this->current_row;
315 return $result[$this->current_row];
318 // --------------------------------------------------------------------
321 * The following functions are normally overloaded by the identically named
322 * methods in the platform-specific driver -- except when query caching
323 * is used. When caching is enabled we do not load the other driver.
324 * These functions are primarily here to prevent undefined function errors
325 * when a cached result object is in use. They are not otherwise fully
326 * operational due to the unavailability of the database resource IDs with
329 function num_rows() { return $this->num_rows; }
330 function num_fields() { return 0; }
331 function list_fields() { return array(); }
332 function field_data() { return array(); }
333 function free_result() { return TRUE; }
334 function _data_seek() { return TRUE; }
335 function _fetch_assoc() { return array(); }
336 function _fetch_object() { return array(); }
339 // END DB_result class
341 /* End of file DB_result.php */
342 /* Location: ./system/database/DB_result.php */