Welcome Guest, Not a member yet? Register   Sign In
DB Help
#1

[eluser]Jagar[/eluser]
I'm not sure what i'm doing wrong, but it comes out wrong.

I'm trying to query using active record class using the following query:
Code:
$query = $this->db->select("if(status=1,'Enabled','Disabled') as status")->from('admin')->get();
but I get the following error:

Code:
A Database Error Occurred

Error Number: 1064

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 'as status FROM (`admin`)' at line 1

SELECT if(status=1, `'Enabled'`, `'Disabled')` as status FROM (`admin`)

I'm translating 0 to Disabled and 1 to Enabled

Thanks
#2

[eluser]Jagar[/eluser]
If I set the second parameter of select to false, all works, but is there anyway to get it working without that parameter?

Thanks
#3

[eluser]Dam1an[/eluser]
No, the reason you need the third paramter is to prevent CI from trying to protect nested brackets
Quote:$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.
#4

[eluser]Jagar[/eluser]
So is there a way to get my query working without setting it to false?
#5

[eluser]Dam1an[/eluser]
Nope, unless you want to try setting the default paramter to false, but that'll innevitable break something else where

The other option is to use pure raw SQL for that query
#6

[eluser]Jagar[/eluser]
All right thanks, I'll try that




Theme © iAndrew 2016 - Forum software by © MyBB