Field Data
Retrieving Field Names
Sometimes it's helpful to gather the field names for a particular table or with a paritcular query result.
$this->db->field_names();
Returns an array containing the field names. This query can be called two ways:
1. You can supply the table name and call it from the $this->db-> object:
$fields = $this->db->field_names('table_name');
foreach ($fields as $field)
{
echo $field;
}
2. You can gather the feild names associated with any query you run by calling the function from your query result object:
$query = $this->db->query('SELECT * FROM some_table');
foreach ($query->field_names() 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:
- name - column name
- max_length - maximum length of the column
- primary_key - 1 if the column is a primary key
- type - the type of the column