magnolia
magnolia

Reputation: 43

Codeigniter concat and join

I want to concat 2 fields together in my codeigniter db query but its not working, can you help me please?

$this->db->select('*, news.id as news_id, news.created as created_on, CONCAT(users.firstname, users.surname) as article_author, CONCAT(modified_user.firstname, modified_user.surname) as modified_author');
$this->db->join('users', 'users.id = news.author', 'left');
$this->db->join('users modified_user', 'modified_user.id = news.modified_by', 'left');  

Thanks

Upvotes: 0

Views: 1805

Answers (3)

nathajamal
nathajamal

Reputation: 90

if you are using CONCAT you need to pass FALSE as an argument after your select statement. Like so:

$this->db->select('*, news.id as news_id, news.created as created_on, CONCAT(users.firstname, users.surname) as article_author, CONCAT(modified_user.firstname, modified_user.surname) as modified_author',FALSE);

$this->db->join('users', 'users.id = news.author', 'left');

$this->db->join('users modified_user', 'modified_user.id = news.modified_by', 'left');

see this answer: concat in php codeigniter

Upvotes: 0

juanrossi
juanrossi

Reputation: 305

The code you posted is the complete one? Try adding the get function:

$this->db->select('*, news.id as news_id, news.created as created_on, CONCAT(users.firstname, users.surname) as article_author, CONCAT(modified_user.firstname, modified_user.surname) as modified_author')->join('users', 'users.id = news.author', 'left')->join('users modified_user', 'modified_user.id = news.modified_by', 'left');
$this->db->get('TableName');

Do you get any error? You could also try printing the query with:

print_r( $this->db->last_query() );

Upvotes: 0

cwallenpoole
cwallenpoole

Reputation: 82018

At a minimum, you're missing a ->from. This means you have nothing to join to.

Upvotes: 3

Related Questions