Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########## |
| 2 | Field Data |
| 3 | ########## |
| 4 | |
| 5 | $this->db->list_fields() |
| 6 | ========================= |
| 7 | |
| 8 | Returns an array containing the field names. This query can be called |
| 9 | two ways: |
| 10 | |
| 11 | 1. You can supply the table name and call it from the $this->db-> |
| 12 | object:: |
| 13 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 14 | $fields = $this->db->list_fields('table_name'); |
| 15 | |
| 16 | foreach ($fields as $field) |
| 17 | { |
| 18 | echo $field; |
| 19 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 20 | |
| 21 | 2. You can gather the field names associated with any query you run by |
| 22 | calling the function from your query result object:: |
| 23 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 24 | $query = $this->db->query('SELECT * FROM some_table'); |
| 25 | |
| 26 | foreach ($query->list_fields() as $field) |
| 27 | { |
| 28 | echo $field; |
| 29 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
| 31 | $this->db->field_exists() |
| 32 | ========================== |
| 33 | |
| 34 | Sometimes it's helpful to know whether a particular field exists before |
| 35 | performing an action. Returns a boolean TRUE/FALSE. Usage example:: |
| 36 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 37 | if ($this->db->field_exists('field_name', 'table_name')) |
| 38 | { |
| 39 | // some code... |
| 40 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 41 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 42 | .. note:: Replace *field_name* with the name of the column you are looking |
| 43 | for, and replace *table_name* with the name of the table you are |
| 44 | looking for. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 45 | |
| 46 | $this->db->field_data() |
| 47 | ======================== |
| 48 | |
| 49 | Returns an array of objects containing field information. |
| 50 | |
| 51 | Sometimes it's helpful to gather the field names or other metadata, like |
| 52 | the column type, max length, etc. |
| 53 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 54 | .. note:: Not all databases provide meta-data. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 55 | |
| 56 | Usage example:: |
| 57 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 58 | $fields = $this->db->field_data('table_name'); |
| 59 | |
| 60 | foreach ($fields as $field) |
| 61 | { |
| 62 | echo $field->name; |
| 63 | echo $field->type; |
| 64 | echo $field->max_length; |
| 65 | echo $field->primary_key; |
| 66 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 67 | |
| 68 | If you have run a query already you can use the result object instead of |
| 69 | supplying the table name:: |
| 70 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 71 | $query = $this->db->query("YOUR QUERY"); |
| 72 | $fields = $query->field_data(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 73 | |
| 74 | The following data is available from this function if supported by your |
| 75 | database: |
| 76 | |
| 77 | - name - column name |
| 78 | - max_length - maximum length of the column |
| 79 | - primary_key - 1 if the column is a primary key |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 80 | - type - the type of the column |