Skip to content
Advertisement

Cast a column as Boolean in PDO

I have a field (‘done’) in the Database which is boolean. The function which GET/ the rows doesn’t return a json with that field as boolean but it returns 1 or 0.

JavaScript

How can I get THAT column (‘done’ column) as boolean? I mean, Is posible to cast a column with PDO::PARAM_BOOL ??

Here is my sample of UPDATE that inserts booleans in the database and it works perfectly:

JavaScript

Advertisement

Answer

In SQL there are no booleans really, as you notice the 1 and 0 .

The right terminology is BIT field. you should not try to modify it, if you retrieve it as 1 and 0 then you should insert it as 1 or 0.

Also note that the JSON in the catch will produce is not valid, never build json manually. So change :

JavaScript

to

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement