Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * MySQL Result Class |
| 30 | * |
| 31 | * This class extends the parent result class: CI_DB_result |
| 32 | * |
| 33 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/database/ |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 36 | * @since 1.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | */ |
| 38 | class CI_DB_mysql_result extends CI_DB_result { |
| 39 | |
| 40 | /** |
Andrey Andreev | 35c7adc | 2012-07-05 18:06:32 +0300 | [diff] [blame^] | 41 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 58 | * Number of rows in the result set |
| 59 | * |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 60 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 62 | public function num_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | { |
Andrey Andreev | 35c7adc | 2012-07-05 18:06:32 +0300 | [diff] [blame^] | 64 | return $this->num_rows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 66 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | // -------------------------------------------------------------------- |
| 68 | |
| 69 | /** |
| 70 | * Number of fields in the result set |
| 71 | * |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 72 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 74 | public function num_fields() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | { |
| 76 | return @mysql_num_fields($this->result_id); |
| 77 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 78 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | // -------------------------------------------------------------------- |
| 80 | |
| 81 | /** |
| 82 | * Fetch Field Names |
| 83 | * |
| 84 | * Generates an array of column names |
| 85 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | * @return array |
| 87 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 88 | public function list_fields() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | { |
| 90 | $field_names = array(); |
| 91 | while ($field = mysql_fetch_field($this->result_id)) |
| 92 | { |
| 93 | $field_names[] = $field->name; |
| 94 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | return $field_names; |
| 97 | } |
| 98 | |
| 99 | // -------------------------------------------------------------------- |
| 100 | |
| 101 | /** |
| 102 | * Field data |
| 103 | * |
| 104 | * Generates an array of objects containing field meta-data |
| 105 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | * @return array |
| 107 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 108 | public function field_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | { |
| 110 | $retval = array(); |
Andrey Andreev | effd013 | 2012-03-02 12:34:54 +0200 | [diff] [blame] | 111 | for ($i = 0, $c = $this->num_fields(); $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 = 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 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 123 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | // -------------------------------------------------------------------- |
| 125 | |
| 126 | /** |
| 127 | * Free the result |
| 128 | * |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 129 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 131 | public function free_result() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | { |
| 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 Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 145 | * Moves the internal pointer to the desired offset. We call |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | * this internally before fetching results to make sure the |
| 147 | * result set starts at zero |
| 148 | * |
Andrey Andreev | 8f3566f | 2012-04-12 14:30:05 +0300 | [diff] [blame] | 149 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 151 | protected function _data_seek($n = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | { |
Andrey Andreev | 35c7adc | 2012-07-05 18:06:32 +0300 | [diff] [blame^] | 153 | return $this->num_rows |
| 154 | ? @mysql_data_seek($this->result_id, $n) |
| 155 | : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // -------------------------------------------------------------------- |
| 159 | |
| 160 | /** |
| 161 | * Result - associative array |
| 162 | * |
| 163 | * Returns the result set as an array |
| 164 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | * @return array |
| 166 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 167 | protected function _fetch_assoc() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | { |
| 169 | return mysql_fetch_assoc($this->result_id); |
| 170 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 171 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
| 175 | * Result - object |
| 176 | * |
| 177 | * Returns the result set as an object |
| 178 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | * @return object |
| 180 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 181 | protected function _fetch_object() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | { |
| 183 | return mysql_fetch_object($this->result_id); |
| 184 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 185 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | /* End of file mysql_result.php */ |
Timothy Warren | 9cc5c60 | 2012-03-19 18:36:37 -0400 | [diff] [blame] | 189 | /* Location: ./system/database/drivers/mysql/mysql_result.php */ |