简体   繁体   中英

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

Try:

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

From manual

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM