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 | |
| 14 | $fields = $this->db->list_fields('table_name'); foreach ($fields as $field) { echo $field; } |
| 15 | |
| 16 | 2. You can gather the field names associated with any query you run by |
| 17 | calling the function from your query result object:: |
| 18 | |
| 19 | $query = $this->db->query('SELECT * FROM some_table'); foreach ($query->list_fields() as $field) { echo $field; } |
| 20 | |
| 21 | $this->db->field_exists() |
| 22 | ========================== |
| 23 | |
| 24 | Sometimes it's helpful to know whether a particular field exists before |
| 25 | performing an action. Returns a boolean TRUE/FALSE. Usage example:: |
| 26 | |
| 27 | if ($this->db->field_exists('field_name', 'table_name')) { // some code... } |
| 28 | |
| 29 | Note: Replace *field_name* with the name of the column you are looking |
| 30 | for, and replace *table_name* with the name of the table you are |
| 31 | looking for. |
| 32 | |
| 33 | $this->db->field_data() |
| 34 | ======================== |
| 35 | |
| 36 | Returns an array of objects containing field information. |
| 37 | |
| 38 | Sometimes it's helpful to gather the field names or other metadata, like |
| 39 | the column type, max length, etc. |
| 40 | |
| 41 | Note: Not all databases provide meta-data. |
| 42 | |
| 43 | Usage example:: |
| 44 | |
| 45 | $fields = $this->db->field_data('table_name'); foreach ($fields as $field) { echo $field->name; echo $field->type; echo $field->max_length; echo $field->primary_key; } |
| 46 | |
| 47 | If you have run a query already you can use the result object instead of |
| 48 | supplying the table name:: |
| 49 | |
| 50 | $query = $this->db->query("YOUR QUERY"); $fields = $query->field_data(); |
| 51 | |
| 52 | The following data is available from this function if supported by your |
| 53 | database: |
| 54 | |
| 55 | - name - column name |
| 56 | - max_length - maximum length of the column |
| 57 | - primary_key - 1 if the column is a primary key |
| 58 | - type - the type of the column |
| 59 | |