Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get table structure in CodeIgniter

I want to know the structure of a table. How I can do it in CodeIgniter. Using database class I got 'Invalid SQL Statement' error when I ran $this->db->query('desc mytable');

like image 691
Lhuqita Fazry Avatar asked Aug 18 '11 05:08

Lhuqita Fazry


2 Answers

Try:

$fields = $this->db->list_fields('table_name');
foreach ($fields as $field)
{
   echo $field;
}

From manual

like image 196
Alfonso Rubalcava Avatar answered Nov 01 '22 11:11

Alfonso Rubalcava


For more descriptive information, you should use

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

You're going to get something like this foreach field in fields as stdClass

name = "id"
type = "int"
max_length = 11
default = null
primary_key = 1
like image 28
Omar Alvarado Avatar answered Nov 01 '22 10:11

Omar Alvarado