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