04d8b8096be0c1fed546e53bf31e957d395c67a0
[living-lab-site.git] / user_guide / database / fields.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3 <head>
4
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <title>Field Data : CodeIgniter User Guide</title>
7
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
11 <script type="text/javascript" src="../nav/nav.js"></script>
12 <script type="text/javascript" src="../nav/prototype.lite.js"></script>
13 <script type="text/javascript" src="../nav/moo.fx.js"></script>
14 <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
16 <meta http-equiv='expires' content='-1' />
17 <meta http-equiv= 'pragma' content='no-cache' />
18 <meta name='robots' content='all' />
19 <meta name='author' content='ExpressionEngine Dev Team' />
20 <meta name='description' content='CodeIgniter User Guide' />
21
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_darker.jpg" width="154" height="43" 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>
31 <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
32 <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33 </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">
42 <a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
43 <a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
44 <a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
45 Field Names
46 </td>
47 <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>
48 </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
63 <h2>$this->db->list_fields()</h2>
64 <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>
68
69 <code>
70 $fields = $this->db->list_fields('table_name');<br /><br />
71
72 foreach ($fields as $field)<br />
73 {<br />
74 &nbsp;&nbsp;&nbsp;echo $field;<br />
75 }
76 </code>
77
78 <p>2. You can gather the field names associated with any query you run by calling the function
79 from your query result object:</p>
80
81 <code>
82 $query = $this->db->query('SELECT * FROM some_table');
83 <br /><br />
84
85 foreach ($query->list_fields() as $field)<br />
86 {<br />
87 &nbsp;&nbsp;&nbsp;echo $field;<br />
88 }
89 </code>
90
91
92 <h2>$this->db->field_exists()</h2>
93
94 <p>Sometimes it's helpful to know whether a particular field exists before performing an action.
95 Returns a boolean TRUE/FALSE.  Usage example:</p>
96
97 <code>
98 if ($this->db->field_exists('field_name', 'table_name'))<br />
99 {<br />
100 &nbsp;&nbsp; // some code...<br />
101 }
102 </code>
103
104 <p>Note:  Replace <em>field_name</em> with the name of the column you are looking for, and replace
105 <em>table_name</em> with the name of the table you are looking for.</p>
106
107
108 <h2>$this->db->field_data()</h2>
109 <p>Returns an array of objects containing field information.</p>
110 <p>Sometimes it's helpful to gather the field names or other metadata, like the column type, max length, etc.</p>
111
112
113 <p class="important">Note: Not all databases provide meta-data.</p>
114
115 <p>Usage example:</p>
116
117 <code>
118 $fields = $this->db->field_data('table_name');<br /><br />
119
120 foreach ($fields as $field)<br />
121 {<br />
122 &nbsp;&nbsp;&nbsp;echo $field->name;<br />
123 &nbsp;&nbsp;&nbsp;echo $field->type;<br />
124 &nbsp;&nbsp;&nbsp;echo $field->max_length;<br />
125 &nbsp;&nbsp;&nbsp;echo $field->primary_key;<br />
126 }
127 </code>
128
129 <p>If you have run a query already you can use the result object instead of supplying the table name:</p>
130
131 <code>
132 $query = $this->db->query("YOUR QUERY");<br />
133 $fields = $query->field_data();
134 </code>
135
136
137 <p>The following data is available from this function if supported by your database:</p>
138
139 <ul>
140 <li>name - column name</li>
141 <li>max_length - maximum length of the column</li>
142 <li>primary_key - 1 if the column is a primary key</li>
143 <li>type - the type of the column</li>
144 </ul>
145
146
147 </div>
148 <!-- END CONTENT -->
149
150
151 <div id="footer">
152 <p>
153 Previous Topic:&nbsp;&nbsp;<a href="table_data.html"> Table Data</a>
154 &nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
155 <a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
156 <a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
157 Next Topic:&nbsp;&nbsp;<a href="call_function.html">Custom Function Calls</a>
158 </p>
159 <p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
160 </div>
161
162 </body>
163 </html>