How to get table structure in CodeIgniter

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

+6
source share
3 answers

Try:

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

From the manual

+13
source

For more details you should use

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

You will get something like this foreach field in fields like stdClass

 name = "id" type = "int" max_length = 11 default = null primary_key = 1 
+2
source

To get the table schema in a CodeIgniter query:

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

Source: https://habr.com/ru/post/895298/


All Articles