Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get field names of mysql table in codeigniter?

i am new in codeigniter. And i am trying to get field name of a table with a query.

I have write a query

"select * from user"

and pass it to $this->db->query() function. i am getting records. But i want to get field names of table. so how can i get that?

Can anybody help me please. Thanks in advance.

like image 654
Chirayu Avatar asked Apr 21 '12 10:04

Chirayu


2 Answers

using database library write this code to list all fields:

$this->db->list_fields('table')

take a look here: https://codeigniter.com/userguide3/database/results.html#CI_DB_result::list_fields

like image 51
semsem Avatar answered Oct 21 '22 21:10

semsem


What you did is to get the datas from the table.... here your table is user so

in your model function do this...

function get_field()
{
$result = $this->db->list_fields('user');
foreach($result as $field)
{
$data[] = $field;
return $data;
}
}

in your controller do this

function get_field()
{
$data['field'] = $this->model_name->get_field();
$this->load->view('view_name',$data);
}

in your view do this

foreach($field as $f)
{
echo $f."<br>"; //this will echo all your fields
}

hope this will help you

like image 41
Anwar Avatar answered Oct 21 '22 19:10

Anwar