Question

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?

Was it helpful?

Solution

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);

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top