admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
Derek Allard | afd99ac | 2008-01-19 19:59:14 +0000 | [diff] [blame] | 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 3 | <head>
|
| 4 |
|
Derek Allard | 404e35d | 2007-08-07 01:00:45 +0000 | [diff] [blame] | 5 | <title>CodeIgniter User Guide : Field Data</title>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 6 |
|
| 7 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 8 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 9 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 14 |
|
| 15 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 16 | <meta http-equiv='expires' content='-1' />
|
| 17 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 18 | <meta name='robots' content='all' />
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 19 | <meta name='author' content='ExpressionEngine Dev Team' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 20 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 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.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 Allard | 197d10b | 2008-02-12 04:20:38 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.6.1</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 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">
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 42 | <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 43 | <a href="../index.html">User Guide Home</a> ›
|
| 44 | <a href="index.html">Database Library</a> ›
|
| 45 | Field Names
|
| 46 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 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 <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 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 |
|
admin | 194b894 | 2006-10-04 07:59:32 +0000 | [diff] [blame] | 63 | <h2>$this->db->list_fields()</h2>
|
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 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>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 68 |
|
| 69 | <code>
|
admin | 194b894 | 2006-10-04 07:59:32 +0000 | [diff] [blame] | 70 | $fields = $this->db->list_fields('table_name')<br /><br />
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 71 |
|
| 72 | foreach ($fields as $field)<br />
|
| 73 | {<br />
|
| 74 | echo $field;<br />
|
| 75 | }
|
| 76 | </code>
|
| 77 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 78 | <p>2. You can gather the field names associated with any query you run by calling the function
|
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 79 | from your query result object:</p>
|
| 80 |
|
| 81 | <code>
|
admin | 194b894 | 2006-10-04 07:59:32 +0000 | [diff] [blame] | 82 | $query = $this->db->query('SELECT * FROM some_table')
|
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 83 | <br /><br />
|
| 84 |
|
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 85 | foreach ($query->list_fields() as $field)<br />
|
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 86 | {<br />
|
| 87 | echo $field;<br />
|
| 88 | }
|
| 89 | </code>
|
| 90 |
|
| 91 |
|
admin | 194b894 | 2006-10-04 07:59:32 +0000 | [diff] [blame] | 92 | <h2>$this->db->field_exists()</h2>
|
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 93 |
|
admin | e7e1dcd | 2006-10-21 18:04:01 +0000 | [diff] [blame] | 94 | <p>Sometimes it's helpful to know whether a particular field exists before performing an action.
|
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 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 | // some code...<br />
|
| 101 | }
|
| 102 | </code>
|
| 103 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 104 | <p>Note: Replace <em>field_name</em> with the name of the column you are looking for, and replace
|
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 105 | <em>table_name</em> with the name of the table you are looking for.</p>
|
| 106 |
|
| 107 |
|
| 108 |
|
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 109 |
|
admin | 194b894 | 2006-10-04 07:59:32 +0000 | [diff] [blame] | 110 | <h2>$this->db->field_data()</h2>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 111 | <p>Returns an array of objects containing field information.</p>
|
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 112 | <p>Sometimes it's helpful to gather the field names or other metadata, like the column type, max length, etc.</p>
|
| 113 |
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 114 |
|
| 115 | <p class="important">Note: Not all databases provide meta-data.</p>
|
| 116 |
|
| 117 | <p>Usage example:</p>
|
| 118 |
|
| 119 | <code>
|
admin | 194b894 | 2006-10-04 07:59:32 +0000 | [diff] [blame] | 120 | $fields = $this->db->field_data('table_name')<br /><br />
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 121 |
|
| 122 | foreach ($fields as $field)<br />
|
| 123 | {<br />
|
| 124 | echo $field->name;<br />
|
| 125 | echo $field->type;<br />
|
| 126 | echo $field->max_length;<br />
|
| 127 | echo $field->primary_key;<br />
|
| 128 | }
|
| 129 | </code>
|
| 130 |
|
admin | e7e1dcd | 2006-10-21 18:04:01 +0000 | [diff] [blame] | 131 | <p>If you have run a query already you can use the result object instead of supplying the table name:</p>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 132 |
|
| 133 | <code>
|
admin | 194b894 | 2006-10-04 07:59:32 +0000 | [diff] [blame] | 134 | $query = $this->db->query("YOUR QUERY")<br />
|
| 135 | $fields = $query->field_data()
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 136 | </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 Allard | 9da4dbc | 2007-04-03 11:39:35 +0000 | [diff] [blame] | 158 | Previous Topic: <a href="table_data.html"> Table Data</a>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 159 | ·
|
| 160 | <a href="#top">Top of Page</a> ·
|
| 161 | <a href="../index.html">User Guide Home</a> ·
|
admin | d125c5c | 2006-09-25 23:26:12 +0000 | [diff] [blame] | 162 | Next Topic: <a href="call_function.html">Custom Function Calls</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 163 | </p>
|
Derek Jones | 0787043 | 2008-02-13 03:49:26 +0000 | [diff] [blame] | 164 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2008 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | fb28bb8 | 2006-09-24 17:59:33 +0000 | [diff] [blame] | 165 | </div>
|
| 166 |
|
| 167 | </body>
|
| 168 | </html> |