Code Igniter User Guide Version 1.4.1


Field Data

Retrieving Field Names

Sometimes it's helpful to gather the field names.

$this->db->field_names();

Returns an array containing the field names. You must supply the table name to the function:

$fields = $this->db->field_names('table_name');

foreach ($fields as $field)
{
   echo $field;
}

Retrieving Field MetaData

Sometimes it's helpful to gather the field names or other metadata, like the column type, max length, etc.

$this->db->field_data();

Returns an array of objects containing field information.

Note: Not all databases provide meta-data.

Usage example:

$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;
}

If you have run a query already you can use the result oject instead of supplying the table name:

$query = $this->db->query("YOUR QUERY");
$fields = $query->field_data();

The following data is available from this function if supported by your database: