Code igniter always escaping my SELECT … CASE quer

2019-05-14 03:50发布

问题:

I am using Codeigniter to fire a query on my db. The code looks like:

$this->db->select("category.Name,
booking.Comment,
CASE WHEN amount > 0 THEN amount END AS PosAmount,
CASE WHEN amount < 0 THEN amount END AS NegAmount");

But I always get an

You have an error in your SQL syntax ... right syntax to use near
'WHEN amount > 0 THEN amount END AS PosAmount, `CASE` WHEN amount < 0 
THEN amount' at line 1

Codeigniter is escaping the CASE but I do not know how to prevent this.

Any ideas?

回答1:

From the documentation :

$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. This is useful if you need a compound select statement where automatic escaping of fields may break them.

In your case :

$this->db->select("category.Name,
booking.Comment,
CASE WHEN amount > 0 THEN amount END AS PosAmount,
CASE WHEN amount < 0 THEN amount END AS NegAmount", FALSE);

This second parameter can also be used in where and join clauses.



标签: codeigniter