blob: 07d51878278cb218df5fc684fb5544d67f1bbc5b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev2caf2892012-01-27 00:12:03 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev2caf2892012-01-27 00:12:03 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * MySQL Result Class
42 *
43 * This class extends the parent result class: CI_DB_result
44 *
45 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49class CI_DB_mysql_result extends CI_DB_result {
50
51 /**
Andrey Andreev8463b912012-11-02 02:16:28 +020052 * Class constructor
Andrey Andreev35c7adc2012-07-05 18:06:32 +030053 *
Andrey Andreev8463b912012-11-02 02:16:28 +020054 * @param object &$driver_object
Andrey Andreev35c7adc2012-07-05 18:06:32 +030055 * @return void
56 */
57 public function __construct(&$driver_object)
58 {
59 parent::__construct($driver_object);
60
61 // Required, due to mysql_data_seek() causing nightmares
62 // with empty result sets
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +030063 $this->num_rows = mysql_num_rows($this->result_id);
Andrey Andreev35c7adc2012-07-05 18:06:32 +030064 }
65
66 // --------------------------------------------------------------------
67
68 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000069 * Number of rows in the result set
70 *
Andrey Andreev2caf2892012-01-27 00:12:03 +020071 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000072 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020073 public function num_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +000074 {
Andrey Andreev35c7adc2012-07-05 18:06:32 +030075 return $this->num_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +000076 }
Barry Mienydd671972010-10-04 16:33:58 +020077
Derek Allard2067d1a2008-11-13 22:59:24 +000078 // --------------------------------------------------------------------
79
80 /**
81 * Number of fields in the result set
82 *
Andrey Andreev2caf2892012-01-27 00:12:03 +020083 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000084 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020085 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +030087 return mysql_num_fields($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +000088 }
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 // --------------------------------------------------------------------
91
92 /**
93 * Fetch Field Names
94 *
95 * Generates an array of column names
96 *
Derek Allard2067d1a2008-11-13 22:59:24 +000097 * @return array
98 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020099 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 $field_names = array();
Chris Buckleyb835a4f2013-01-28 23:35:13 +0000102 mysql_field_seek($this->result_id, 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 while ($field = mysql_fetch_field($this->result_id))
104 {
105 $field_names[] = $field->name;
106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 return $field_names;
109 }
110
111 // --------------------------------------------------------------------
112
113 /**
114 * Field data
115 *
116 * Generates an array of objects containing field meta-data
117 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 * @return array
119 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200120 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
122 $retval = array();
Andrey Andreeveffd0132012-03-02 12:34:54 +0200123 for ($i = 0, $c = $this->num_fields(); $i < $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200124 {
Andrey Andreeveffd0132012-03-02 12:34:54 +0200125 $retval[$i] = new stdClass();
126 $retval[$i]->name = mysql_field_name($this->result_id, $i);
127 $retval[$i]->type = mysql_field_type($this->result_id, $i);
128 $retval[$i]->max_length = mysql_field_len($this->result_id, $i);
Andrey Andreev10ecc842012-11-16 01:06:20 +0200129 $retval[$i]->primary_key = (int) (strpos(mysql_field_flags($this->result_id, $i), 'primary_key') !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 return $retval;
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // --------------------------------------------------------------------
136
137 /**
138 * Free the result
139 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200140 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200141 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200142 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
144 if (is_resource($this->result_id))
145 {
146 mysql_free_result($this->result_id);
147 $this->result_id = FALSE;
148 }
149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Data Seek
155 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200156 * Moves the internal pointer to the desired offset. We call
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * this internally before fetching results to make sure the
Andrey Andreev8463b912012-11-02 02:16:28 +0200158 * result set starts at zero.
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200160 * @param int $n
Andrey Andreev8f3566f2012-04-12 14:30:05 +0300161 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 */
Andrey Andreev69edc432012-12-04 13:32:16 +0200163 public function data_seek($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
Andrey Andreev35c7adc2012-07-05 18:06:32 +0300165 return $this->num_rows
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300166 ? mysql_data_seek($this->result_id, $n)
Andrey Andreev35c7adc2012-07-05 18:06:32 +0300167 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Result - associative array
174 *
175 * Returns the result set as an array
176 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 * @return array
178 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200179 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 return mysql_fetch_assoc($this->result_id);
182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 // --------------------------------------------------------------------
185
186 /**
187 * Result - object
188 *
189 * Returns the result set as an object
190 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200191 * @param string $class_name
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @return object
193 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300194 protected function _fetch_object($class_name = 'stdClass')
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300196 return mysql_fetch_object($this->result_id, $class_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199}