Reputation: 5586
I want to select multiple columns in a table using codeIgniter active records. Selecting using
$this->db->select('*');
$this->db->from($this->table_name);
but selecting a number of columns e.g.
$this->db->select('username','email','last_login','created','modified','group_id');
$this->db->from($this->table_name);
does not work.It only returns an array with the values from the first column. How should I do this?
according to the CodeIgniter user guide they gave the following example. so I thought it should work in my case.
$this->db->select('title, content, date');
$query = $this->db->get('mytable');
// Produces: SELECT title, content, date FROM mytable
Upvotes: 3
Views: 36645
Reputation: 102735
The second query will not work as you expect, as select()
expects a string or array as the first parameter rather than taking unlimited arguments. In your example, only username
would be selected. The correct string syntax is this:
$this->db->select('username, email, last_login, created, modified, group_id');
There's much more I could share with you, but I suggest you have another read or two through the Active Record documentation instead. Good luck, and enjoy Codeigniter!
AFTER YOUR EDIT: Please note the differences in these two examples:
1 - This line passes each column as a separate argument:
$this->db
->select('username','email','last_login','created','modified','group_id');
2 - This line correctly passes one argument (comma separated string):
$this->db
->select('username, email, last_login, created, modified, group_id');
Note the absence of quotes around each column name in the (correct) example 2. In example 1, there are several arguments passed to the function, and only the first one is used, while the rest are ignored.
Upvotes: 7