blob: a75cfad1f41bfd8dd6be4f730f63392fb67bce5d [file] [log] [blame]
Andrey Andreev2caf2892012-01-27 00:12:03 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev2caf2892012-01-27 00:12:03 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev2caf2892012-01-27 00:12:03 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * MySQL Result Class
30 *
31 * This class extends the parent result class: CI_DB_result
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/database/
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030036 * @since 1.0
Derek Allard2067d1a2008-11-13 22:59:24 +000037 */
38class CI_DB_mysql_result extends CI_DB_result {
39
40 /**
Andrey Andreev35c7adc2012-07-05 18:06:32 +030041 * Constructor
42 *
43 * @param object
44 * @return void
45 */
46 public function __construct(&$driver_object)
47 {
48 parent::__construct($driver_object);
49
50 // Required, due to mysql_data_seek() causing nightmares
51 // with empty result sets
52 $this->num_rows = @mysql_num_rows($this->result_id);
53 }
54
55 // --------------------------------------------------------------------
56
57 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000058 * Number of rows in the result set
59 *
Andrey Andreev2caf2892012-01-27 00:12:03 +020060 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000061 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020062 public function num_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +000063 {
Andrey Andreev35c7adc2012-07-05 18:06:32 +030064 return $this->num_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +000065 }
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 // --------------------------------------------------------------------
68
69 /**
70 * Number of fields in the result set
71 *
Andrey Andreev2caf2892012-01-27 00:12:03 +020072 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000073 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020074 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000075 {
76 return @mysql_num_fields($this->result_id);
77 }
Barry Mienydd671972010-10-04 16:33:58 +020078
Derek Allard2067d1a2008-11-13 22:59:24 +000079 // --------------------------------------------------------------------
80
81 /**
82 * Fetch Field Names
83 *
84 * Generates an array of column names
85 *
Derek Allard2067d1a2008-11-13 22:59:24 +000086 * @return array
87 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020088 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
90 $field_names = array();
91 while ($field = mysql_fetch_field($this->result_id))
92 {
93 $field_names[] = $field->name;
94 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 return $field_names;
97 }
98
99 // --------------------------------------------------------------------
100
101 /**
102 * Field data
103 *
104 * Generates an array of objects containing field meta-data
105 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 * @return array
107 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200108 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
110 $retval = array();
Andrey Andreeveffd0132012-03-02 12:34:54 +0200111 for ($i = 0, $c = $this->num_fields(); $i < $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200112 {
Andrey Andreeveffd0132012-03-02 12:34:54 +0200113 $retval[$i] = new stdClass();
114 $retval[$i]->name = mysql_field_name($this->result_id, $i);
115 $retval[$i]->type = mysql_field_type($this->result_id, $i);
116 $retval[$i]->max_length = mysql_field_len($this->result_id, $i);
117 $retval[$i]->primary_key = (strpos(mysql_field_flags($this->result_id, $i), 'primary_key') === FALSE) ? 0 : 1;
118 $retval[$i]->default = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 return $retval;
122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // --------------------------------------------------------------------
125
126 /**
127 * Free the result
128 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200129 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200130 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200131 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
133 if (is_resource($this->result_id))
134 {
135 mysql_free_result($this->result_id);
136 $this->result_id = FALSE;
137 }
138 }
139
140 // --------------------------------------------------------------------
141
142 /**
143 * Data Seek
144 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200145 * Moves the internal pointer to the desired offset. We call
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 * this internally before fetching results to make sure the
147 * result set starts at zero
148 *
Andrey Andreev8f3566f2012-04-12 14:30:05 +0300149 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200151 protected function _data_seek($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
Andrey Andreev35c7adc2012-07-05 18:06:32 +0300153 return $this->num_rows
154 ? @mysql_data_seek($this->result_id, $n)
155 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Result - associative array
162 *
163 * Returns the result set as an array
164 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 * @return array
166 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200167 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 return mysql_fetch_assoc($this->result_id);
170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 // --------------------------------------------------------------------
173
174 /**
175 * Result - object
176 *
177 * Returns the result set as an object
178 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 * @return object
180 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200181 protected function _fetch_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
183 return mysql_fetch_object($this->result_id);
184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186}
187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188/* End of file mysql_result.php */
Timothy Warren9cc5c602012-03-19 18:36:37 -0400189/* Location: ./system/database/drivers/mysql/mysql_result.php */