blob: 68860e60c7df12795d3cb968d265886e9011f780 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * MySQL Result Class
31 *
32 * This class extends the parent result class: CI_DB_result
33 *
34 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/database/
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030037 * @since 1.0
Derek Allard2067d1a2008-11-13 22:59:24 +000038 */
39class CI_DB_mysql_result extends CI_DB_result {
40
41 /**
Andrey Andreev8463b912012-11-02 02:16:28 +020042 * Class constructor
Andrey Andreev35c7adc2012-07-05 18:06:32 +030043 *
Andrey Andreev8463b912012-11-02 02:16:28 +020044 * @param object &$driver_object
Andrey Andreev35c7adc2012-07-05 18:06:32 +030045 * @return void
46 */
47 public function __construct(&$driver_object)
48 {
49 parent::__construct($driver_object);
50
51 // Required, due to mysql_data_seek() causing nightmares
52 // with empty result sets
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +030053 $this->num_rows = mysql_num_rows($this->result_id);
Andrey Andreev35c7adc2012-07-05 18:06:32 +030054 }
55
56 // --------------------------------------------------------------------
57
58 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000059 * Number of rows in the result set
60 *
Andrey Andreev2caf2892012-01-27 00:12:03 +020061 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000062 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020063 public function num_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +000064 {
Andrey Andreev35c7adc2012-07-05 18:06:32 +030065 return $this->num_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +000066 }
Barry Mienydd671972010-10-04 16:33:58 +020067
Derek Allard2067d1a2008-11-13 22:59:24 +000068 // --------------------------------------------------------------------
69
70 /**
71 * Number of fields in the result set
72 *
Andrey Andreev2caf2892012-01-27 00:12:03 +020073 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000074 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020075 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000076 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +030077 return mysql_num_fields($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +000078 }
Barry Mienydd671972010-10-04 16:33:58 +020079
Derek Allard2067d1a2008-11-13 22:59:24 +000080 // --------------------------------------------------------------------
81
82 /**
83 * Fetch Field Names
84 *
85 * Generates an array of column names
86 *
Derek Allard2067d1a2008-11-13 22:59:24 +000087 * @return array
88 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020089 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
91 $field_names = array();
Chris Buckleyb835a4f2013-01-28 23:35:13 +000092 mysql_field_seek($this->result_id, 0);
Derek Allard2067d1a2008-11-13 22:59:24 +000093 while ($field = mysql_fetch_field($this->result_id))
94 {
95 $field_names[] = $field->name;
96 }
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Allard2067d1a2008-11-13 22:59:24 +000098 return $field_names;
99 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Field data
105 *
106 * Generates an array of objects containing field meta-data
107 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 * @return array
109 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200110 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
112 $retval = array();
Andrey Andreeveffd0132012-03-02 12:34:54 +0200113 for ($i = 0, $c = $this->num_fields(); $i < $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200114 {
Andrey Andreeveffd0132012-03-02 12:34:54 +0200115 $retval[$i] = new stdClass();
116 $retval[$i]->name = mysql_field_name($this->result_id, $i);
117 $retval[$i]->type = mysql_field_type($this->result_id, $i);
118 $retval[$i]->max_length = mysql_field_len($this->result_id, $i);
Andrey Andreev10ecc842012-11-16 01:06:20 +0200119 $retval[$i]->primary_key = (int) (strpos(mysql_field_flags($this->result_id, $i), 'primary_key') !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 return $retval;
123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 // --------------------------------------------------------------------
126
127 /**
128 * Free the result
129 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200130 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200131 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200132 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
134 if (is_resource($this->result_id))
135 {
136 mysql_free_result($this->result_id);
137 $this->result_id = FALSE;
138 }
139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Data Seek
145 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200146 * Moves the internal pointer to the desired offset. We call
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 * this internally before fetching results to make sure the
Andrey Andreev8463b912012-11-02 02:16:28 +0200148 * result set starts at zero.
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200150 * @param int $n
Andrey Andreev8f3566f2012-04-12 14:30:05 +0300151 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 */
Andrey Andreev69edc432012-12-04 13:32:16 +0200153 public function data_seek($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Andrey Andreev35c7adc2012-07-05 18:06:32 +0300155 return $this->num_rows
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300156 ? mysql_data_seek($this->result_id, $n)
Andrey Andreev35c7adc2012-07-05 18:06:32 +0300157 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 }
159
160 // --------------------------------------------------------------------
161
162 /**
163 * Result - associative array
164 *
165 * Returns the result set as an array
166 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 * @return array
168 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200169 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 return mysql_fetch_assoc($this->result_id);
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 // --------------------------------------------------------------------
175
176 /**
177 * Result - object
178 *
179 * Returns the result set as an object
180 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200181 * @param string $class_name
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 * @return object
183 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300184 protected function _fetch_object($class_name = 'stdClass')
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300186 return mysql_fetch_object($this->result_id, $class_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189}
190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191/* End of file mysql_result.php */
Timothy Warren9cc5c602012-03-19 18:36:37 -0400192/* Location: ./system/database/drivers/mysql/mysql_result.php */