blob: 06ee20e60c12e0e427c05a3ac14a7b667499ce95 [file] [log] [blame]
adminfb28bb82006-09-24 17:59:33 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Derek Allardafd99ac2008-01-19 19:59:14 +00002<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
adminfb28bb82006-09-24 17:59:33 +00003<head>
4
Derek Allard8039d4c2008-05-31 02:47:56 +00005<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Derek Jonesfd93d222008-05-06 15:18:50 +00006<title>Field Data : CodeIgniter User Guide</title>
adminfb28bb82006-09-24 17:59:33 +00007
8<style type='text/css' media='all'>@import url('../userguide.css');</style>
9<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
admin17a890d2006-09-27 20:42:42 +000011<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000012<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000013<script type="text/javascript" src="../nav/moo.fx.js"></script>
Derek Allardb3412372007-10-25 12:15:16 +000014<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminfb28bb82006-09-24 17:59:33 +000015
adminfb28bb82006-09-24 17:59:33 +000016<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
Derek Allard3d879d52008-01-18 19:41:32 +000019<meta name='author' content='ExpressionEngine Dev Team' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
adminfb28bb82006-09-24 17:59:33 +000021
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
28<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Derek Jones6b7b11f2008-05-06 16:07:27 +000031<td><h1>CodeIgniter User Guide Version 1.6.2</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminfb28bb82006-09-24 17:59:33 +000033</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38<!-- START BREADCRUMB -->
39<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
40<tr>
41<td id="breadcrumb">
Derek Jones7a9193a2008-01-21 18:39:20 +000042<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminfb28bb82006-09-24 17:59:33 +000043<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
44<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
45Field Names
46</td>
Derek Allardbc030912007-06-24 18:25:29 +000047<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
adminfb28bb82006-09-24 17:59:33 +000048</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52
53<br clear="all" />
54
55
56<!-- START CONTENT -->
57<div id="content">
58
59
60<h1>Field Data</h1>
61
62
admin194b8942006-10-04 07:59:32 +000063<h2>$this->db->list_fields()</h2>
adminab4f61b2006-09-25 22:12:32 +000064<p>Returns an array containing the field names. This query can be called two ways:</p>
65
66
67<p>1. You can supply the table name and call it from the <dfn>$this->db-></dfn> object:</p>
adminfb28bb82006-09-24 17:59:33 +000068
69<code>
admin194b8942006-10-04 07:59:32 +000070$fields = $this->db->list_fields('table_name')<br /><br />
adminfb28bb82006-09-24 17:59:33 +000071
72foreach ($fields as $field)<br />
73{<br />
74&nbsp;&nbsp;&nbsp;echo $field;<br />
75}
76</code>
77
admine334c472006-10-21 19:44:22 +000078<p>2. You can gather the field names associated with any query you run by calling the function
adminab4f61b2006-09-25 22:12:32 +000079from your query result object:</p>
80
81<code>
admin194b8942006-10-04 07:59:32 +000082$query = $this->db->query('SELECT * FROM some_table')
adminab4f61b2006-09-25 22:12:32 +000083<br /><br />
84
admin3dd978f2006-09-30 19:24:45 +000085foreach ($query->list_fields() as $field)<br />
adminab4f61b2006-09-25 22:12:32 +000086{<br />
87&nbsp;&nbsp;&nbsp;echo $field;<br />
88}
89</code>
90
91
admin194b8942006-10-04 07:59:32 +000092<h2>$this->db->field_exists()</h2>
adminb2a9cec2006-10-01 03:38:04 +000093
admine7e1dcd2006-10-21 18:04:01 +000094<p>Sometimes it's helpful to know whether a particular field exists before performing an action.
adminb2a9cec2006-10-01 03:38:04 +000095Returns a boolean TRUE/FALSE. Usage example:</p>
96
97<code>
98if ($this->db->field_exists('field_name', 'table_name'))<br />
99{<br />
100&nbsp;&nbsp; // some code...<br />
101}
102</code>
103
admine334c472006-10-21 19:44:22 +0000104<p>Note: Replace <em>field_name</em> with the name of the column you are looking for, and replace
adminb2a9cec2006-10-01 03:38:04 +0000105<em>table_name</em> with the name of the table you are looking for.</p>
106
107
108
adminab4f61b2006-09-25 22:12:32 +0000109
admin194b8942006-10-04 07:59:32 +0000110<h2>$this->db->field_data()</h2>
adminfb28bb82006-09-24 17:59:33 +0000111<p>Returns an array of objects containing field information.</p>
admin3dd978f2006-09-30 19:24:45 +0000112<p>Sometimes it's helpful to gather the field names or other metadata, like the column type, max length, etc.</p>
113
adminfb28bb82006-09-24 17:59:33 +0000114
115<p class="important">Note: Not all databases provide meta-data.</p>
116
117<p>Usage example:</p>
118
119<code>
admin194b8942006-10-04 07:59:32 +0000120$fields = $this->db->field_data('table_name')<br /><br />
adminfb28bb82006-09-24 17:59:33 +0000121
122foreach ($fields as $field)<br />
123{<br />
124&nbsp;&nbsp;&nbsp;echo $field->name;<br />
125&nbsp;&nbsp;&nbsp;echo $field->type;<br />
126&nbsp;&nbsp;&nbsp;echo $field->max_length;<br />
127&nbsp;&nbsp;&nbsp;echo $field->primary_key;<br />
128}
129</code>
130
admine7e1dcd2006-10-21 18:04:01 +0000131<p>If you have run a query already you can use the result object instead of supplying the table name:</p>
adminfb28bb82006-09-24 17:59:33 +0000132
133<code>
admin194b8942006-10-04 07:59:32 +0000134$query = $this->db->query("YOUR QUERY")<br />
135$fields = $query->field_data()
adminfb28bb82006-09-24 17:59:33 +0000136</code>
137
138
139<p>The following data is available from this function if supported by your database:</p>
140
141<ul>
142<li>name - column name</li>
143<li>max_length - maximum length of the column</li>
144<li>primary_key - 1 if the column is a primary key</li>
145<li>type - the type of the column</li>
146</ul>
147
148
149
150
151
152</div>
153<!-- END CONTENT -->
154
155
156<div id="footer">
157<p>
Derek Allard9da4dbc2007-04-03 11:39:35 +0000158Previous Topic:&nbsp;&nbsp;<a href="table_data.html"> Table Data</a>
adminfb28bb82006-09-24 17:59:33 +0000159&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
160<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
161<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admind125c5c2006-09-25 23:26:12 +0000162Next Topic:&nbsp;&nbsp;<a href="call_function.html">Custom Function Calls</a>
Derek Allardc6441282007-07-04 23:54:32 +0000163</p>
Derek Jones07870432008-02-13 03:49:26 +0000164<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminfb28bb82006-09-24 17:59:33 +0000165</div>
166
167</body>
168</html>