Mohammed Sufian
Mohammed Sufian

Reputation: 1781

combine columns into one column mysql

I would like to combine two columns in one column as Fullname, and for that I have written the following code:

    $this->db->select('CONCAT(first_name," ",last_name) AS FullName');
    $this->db->from('customer');
    $this->db->where('user_id',1);
    $query = $this->db->get();
    return $query->result_array();

The resulting query would be:

SELECT CONCAT(first_name," ",last_name) AS FullName 
FROM customer 
WHERE user_id = 1

but when i execute the above code it gives me:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (customer) WHERE user_id = '1' at line 2

I have also tried with concat_ws group_concat but not able to get it work. Can anyone see what I'm doing wrong?

Upvotes: 0

Views: 686

Answers (2)

Carlos Rocha
Carlos Rocha

Reputation: 21

I have been through this before with CI, in my case CI was wrongly creating the query, for example putting simgle quotes where they shouldn't be.

My advice create the query yourself and run it, you could be surprise :P

Upvotes: 0

Sam Dufel
Sam Dufel

Reputation: 17608

By default, CI tries to escape what you pass to db->select() (in case you were passing in user-generated values). You can disable this feature by passing false as a second argument.

$this->db->select('CONCAT(first_name," ",last_name) AS FullName', false);

Upvotes: 3

Related Questions