blob: 293980e97083cbe590fdb0ab715ff40fb7ae177d [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
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 */
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
53 $this->num_rows = @mysql_num_rows($this->result_id);
54 }
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 {
77 return @mysql_num_fields($this->result_id);
78 }
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();
92 while ($field = mysql_fetch_field($this->result_id))
93 {
94 $field_names[] = $field->name;
95 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 return $field_names;
98 }
99
100 // --------------------------------------------------------------------
101
102 /**
103 * Field data
104 *
105 * Generates an array of objects containing field meta-data
106 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 * @return array
108 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200109 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
111 $retval = array();
Andrey Andreeveffd0132012-03-02 12:34:54 +0200112 for ($i = 0, $c = $this->num_fields(); $i < $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200113 {
Andrey Andreeveffd0132012-03-02 12:34:54 +0200114 $retval[$i] = new stdClass();
115 $retval[$i]->name = mysql_field_name($this->result_id, $i);
116 $retval[$i]->type = mysql_field_type($this->result_id, $i);
117 $retval[$i]->max_length = mysql_field_len($this->result_id, $i);
Andrey Andreev10ecc842012-11-16 01:06:20 +0200118 $retval[$i]->primary_key = (int) (strpos(mysql_field_flags($this->result_id, $i), 'primary_key') !== FALSE);
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
Andrey Andreev8463b912012-11-02 02:16:28 +0200147 * result set starts at zero.
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200149 * @param int $n
Andrey Andreev8f3566f2012-04-12 14:30:05 +0300150 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 */
Andrey Andreev69edc432012-12-04 13:32:16 +0200152 public function data_seek($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 {
Andrey Andreev35c7adc2012-07-05 18:06:32 +0300154 return $this->num_rows
155 ? @mysql_data_seek($this->result_id, $n)
156 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Result - associative array
163 *
164 * Returns the result set as an array
165 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 * @return array
167 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200168 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
170 return mysql_fetch_assoc($this->result_id);
171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 // --------------------------------------------------------------------
174
175 /**
176 * Result - object
177 *
178 * Returns the result set as an object
179 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200180 * @param string $class_name
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 * @return object
182 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300183 protected function _fetch_object($class_name = 'stdClass')
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300185 return mysql_fetch_object($this->result_id, $class_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188}
189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190/* End of file mysql_result.php */
Timothy Warren9cc5c602012-03-19 18:36:37 -0400191/* Location: ./system/database/drivers/mysql/mysql_result.php */