Welcome Guest, Not a member yet? Register   Sign In
Issue with CONCAT() in Active Record query
#1

[eluser]bezking[/eluser]
I'm seeing a misplaced quote in a SQL statement generated by Active Record. My code looks like this:

Code:
$this->db->select("o.cust_id, o.cust_name, o.cust_rating, CONCAT( o.cust_rating, ' - ', c.description ) as rating")

And the resulting code is like this:

Quote:SELECT `o`.`cust_id`, `o`.`cust_name`, `o`.`cust_rating`, CONCAT( o.cust_rating, `'` - ', `c`.`description` ) as rating

Note those extra backticks surrounding the first quote of the middle part of the CONCAT statement - those are resulting in a SQL error. Is this something I'm doing wrong here or is it a bug? This is CI version 2.0.
#2

[eluser]C. Jiménez[/eluser]
You can add FALSE to select method to prevent malfunction when doing things like that.

Code:
$this->db->select("o.cust_id, o.cust_name, o.cust_rating, CONCAT( o.cust_rating, ' - ', c.description ) as rating",FALSE)
From user_guide:
$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.
#3

[eluser]bezking[/eluser]
[quote author="C. Jiménez" date="1312467316"]You can add FALSE to select method to prevent malfunction when doing things like that.

Code:
$this->db->select("o.cust_id, o.cust_name, o.cust_rating, CONCAT( o.cust_rating, ' - ', c.description ) as rating",FALSE)
From user_guide:
$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.[/quote]

Thanks. Does this stop the escape for only that line or for the entire query (this is part of a multi-line method-chained active record query)?
#4

[eluser]C. Jiménez[/eluser]
It only prevent escape functions for that single select line.
other db methods, like where, join.. have their own parameter to prevent escape.

If you check code of db->select method it explodes ',' from select string to escape fields.




Theme © iAndrew 2016 - Forum software by © MyBB