Lhuqita Fazry
Lhuqita Fazry

Reputation: 291

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');

Upvotes: 7

Views: 7352

Answers (3)

Harun Or Rashid
Harun Or Rashid

Reputation: 380

For Get Table Schema in CodeIgniter query:

$query = $this->db->query('SHOW CREATE TABLE yourTableName');
$queryResultArray = $query->result_array();
print_r( $queryResultArray[0]['Create Table'] );

Upvotes: 0

Omar Alvarado
Omar Alvarado

Reputation: 1744

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

Upvotes: 2

Alfonso Rubalcava
Alfonso Rubalcava

Reputation: 2247

Try:

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

From manual

Upvotes: 15

Related Questions