Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * 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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * 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 Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.3.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * MySQLi Result Class |
| 42 | * |
| 43 | * This class extends the parent result class: CI_DB_result |
| 44 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 45 | * @package CodeIgniter |
| 46 | * @subpackage Drivers |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 49 | * @link https://codeigniter.com/user_guide/database/ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | */ |
| 51 | class CI_DB_mysqli_result extends CI_DB_result { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 52 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | /** |
| 54 | * Number of rows in the result set |
| 55 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 56 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 58 | public function num_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 60 | return is_int($this->num_rows) |
| 61 | ? $this->num_rows |
| 62 | : $this->num_rows = $this->result_id->num_rows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 64 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | // -------------------------------------------------------------------- |
| 66 | |
| 67 | /** |
| 68 | * Number of fields in the result set |
| 69 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 70 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 72 | public function num_fields() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 74 | return $this->result_id->field_count; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // -------------------------------------------------------------------- |
| 78 | |
| 79 | /** |
| 80 | * Fetch Field Names |
| 81 | * |
| 82 | * Generates an array of column names |
| 83 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | * @return array |
| 85 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 86 | public function list_fields() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | { |
| 88 | $field_names = array(); |
Chris Buckley | b835a4f | 2013-01-28 23:35:13 +0000 | [diff] [blame] | 89 | $this->result_id->field_seek(0); |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 90 | while ($field = $this->result_id->fetch_field()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | { |
| 92 | $field_names[] = $field->name; |
| 93 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | return $field_names; |
| 96 | } |
| 97 | |
| 98 | // -------------------------------------------------------------------- |
| 99 | |
| 100 | /** |
| 101 | * Field data |
| 102 | * |
| 103 | * Generates an array of objects containing field meta-data |
| 104 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | * @return array |
| 106 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 107 | public function field_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | { |
| 109 | $retval = array(); |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 110 | $field_data = $this->result_id->fetch_fields(); |
Andrey Andreev | effd013 | 2012-03-02 12:34:54 +0200 | [diff] [blame] | 111 | for ($i = 0, $c = count($field_data); $i < $c; $i++) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 112 | { |
Andrey Andreev | effd013 | 2012-03-02 12:34:54 +0200 | [diff] [blame] | 113 | $retval[$i] = new stdClass(); |
| 114 | $retval[$i]->name = $field_data[$i]->name; |
Andrey Andreev | 9385cfe | 2017-09-13 13:17:28 +0300 | [diff] [blame] | 115 | $retval[$i]->type = static::_get_field_type($field_data[$i]->type); |
Andrey Andreev | effd013 | 2012-03-02 12:34:54 +0200 | [diff] [blame] | 116 | $retval[$i]->max_length = $field_data[$i]->max_length; |
Andrey Andreev | 9385cfe | 2017-09-13 13:17:28 +0300 | [diff] [blame] | 117 | $retval[$i]->primary_key = (int) ($field_data[$i]->flags & MYSQLI_PRI_KEY_FLAG); |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame] | 118 | $retval[$i]->default = $field_data[$i]->def; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | return $retval; |
| 122 | } |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 123 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | // -------------------------------------------------------------------- |
| 125 | |
| 126 | /** |
Andrey Andreev | 9385cfe | 2017-09-13 13:17:28 +0300 | [diff] [blame] | 127 | * Get field type |
| 128 | * |
| 129 | * Extracts field type info from the bitflags returned by |
| 130 | * mysqli_result::fetch_fields() |
| 131 | * |
| 132 | * @used-by CI_DB_mysqli_result::field_data() |
Andrey Andreev | a919465 | 2017-09-27 18:57:46 +0300 | [diff] [blame] | 133 | * @param int $type |
Andrey Andreev | 9385cfe | 2017-09-13 13:17:28 +0300 | [diff] [blame] | 134 | * @return string |
| 135 | */ |
Andrey Andreev | a919465 | 2017-09-27 18:57:46 +0300 | [diff] [blame] | 136 | private static function _get_field_type($type) |
Andrey Andreev | 9385cfe | 2017-09-13 13:17:28 +0300 | [diff] [blame] | 137 | { |
| 138 | static $map; |
| 139 | isset($map) OR $map = array( |
| 140 | MYSQLI_TYPE_DECIMAL => 'decimal', |
| 141 | MYSQLI_TYPE_BIT => 'bit', |
| 142 | MYSQLI_TYPE_TINY => 'tinyint', |
| 143 | MYSQLI_TYPE_SHORT => 'smallint', |
| 144 | MYSQLI_TYPE_INT24 => 'mediumint', |
| 145 | MYSQLI_TYPE_LONG => 'int', |
| 146 | MYSQLI_TYPE_LONGLONG => 'bigint', |
| 147 | MYSQLI_TYPE_FLOAT => 'float', |
| 148 | MYSQLI_TYPE_DOUBLE => 'double', |
| 149 | MYSQLI_TYPE_TIMESTAMP => 'timestamp', |
| 150 | MYSQLI_TYPE_DATE => 'date', |
| 151 | MYSQLI_TYPE_TIME => 'time', |
| 152 | MYSQLI_TYPE_DATETIME => 'datetime', |
| 153 | MYSQLI_TYPE_YEAR => 'year', |
| 154 | MYSQLI_TYPE_NEWDATE => 'date', |
| 155 | MYSQLI_TYPE_INTERVAL => 'interval', |
| 156 | MYSQLI_TYPE_ENUM => 'enum', |
| 157 | MYSQLI_TYPE_SET => 'set', |
| 158 | MYSQLI_TYPE_TINY_BLOB => 'tinyblob', |
| 159 | MYSQLI_TYPE_MEDIUM_BLOB => 'mediumblob', |
| 160 | MYSQLI_TYPE_BLOB => 'blob', |
| 161 | MYSQLI_TYPE_LONG_BLOB => 'longblob', |
| 162 | MYSQLI_TYPE_STRING => 'char', |
| 163 | MYSQLI_TYPE_VAR_STRING => 'varchar', |
| 164 | MYSQLI_TYPE_GEOMETRY => 'geometry' |
| 165 | ); |
| 166 | |
Andrey Andreev | a919465 | 2017-09-27 18:57:46 +0300 | [diff] [blame] | 167 | return isset($map[$type]) ? $map[$type] : $type; |
Andrey Andreev | 9385cfe | 2017-09-13 13:17:28 +0300 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // -------------------------------------------------------------------- |
| 171 | |
| 172 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | * Free the result |
| 174 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 175 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 176 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 177 | public function free_result() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | { |
| 179 | if (is_object($this->result_id)) |
| 180 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 181 | $this->result_id->free(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | $this->result_id = FALSE; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // -------------------------------------------------------------------- |
| 187 | |
| 188 | /** |
| 189 | * Data Seek |
| 190 | * |
Andrey Andreev | 8f3566f | 2012-04-12 14:30:05 +0300 | [diff] [blame] | 191 | * Moves the internal pointer to the desired offset. We call |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | * this internally before fetching results to make sure the |
Andrey Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame] | 193 | * result set starts at zero. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | * |
Andrey Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame] | 195 | * @param int $n |
Andrey Andreev | 8f3566f | 2012-04-12 14:30:05 +0300 | [diff] [blame] | 196 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | */ |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 198 | public function data_seek($n = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 200 | return $this->result_id->data_seek($n); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // -------------------------------------------------------------------- |
| 204 | |
| 205 | /** |
| 206 | * Result - associative array |
| 207 | * |
| 208 | * Returns the result set as an array |
| 209 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | * @return array |
| 211 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 212 | protected function _fetch_assoc() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 214 | return $this->result_id->fetch_assoc(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | // -------------------------------------------------------------------- |
| 218 | |
| 219 | /** |
| 220 | * Result - object |
| 221 | * |
| 222 | * Returns the result set as an object |
| 223 | * |
Andrey Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame] | 224 | * @param string $class_name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | * @return object |
| 226 | */ |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 227 | protected function _fetch_object($class_name = 'stdClass') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | { |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 229 | return $this->result_id->fetch_object($class_name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 231 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | } |