Answer the question
In order to leave comments, you need to log in
Why doesn't TRUE work in SQL query?
Good afternoon, I have a table with users:
I need to get ordinary users and managers from it (column is_manager).
To get regular users, I use this query and it works:
But when I try to get all managers, the query still returns regular users:
Why doesn't TRUE work? How to fix it? SELECT * FROM users WHERE is_manager = "FALSE";
SELECT * FROM users WHERE is_manager = "TRUE";
Answer the question
In order to leave comments, you need to log in
I'll answer the "why" question.
Because type casting.
The string "FALSE" is not the boolean value FALSE, but a string, 5 letters long. It has nothing to do with boolean values or numbers. When comparing with a number, both PHP and mysql try to cast the string to a number. At the same time, 0 is obtained from the "FALSE" string. And therefore the comparison "works". But of course you can't compare like that.
The string "TRUE" is, as it should be clear by now, not the boolean value TRUE, but a string, 4 letters long. It has nothing to do with boolean values or numbers. When comparing with a number, both PHP and mysql try to cast the string to a number. At the same time, 0 is obtained from the string "TRUE". And therefore the comparison "does not work". If you pass not a string but a boolean value TRUE, it will work.
Since there is no boolean field type in mysql, it's best not to show off with TRUE and FALSE, but just pass 0 and 1.
As the author pointed out above, it is not necessary to write the TRUE / FALSE conditions in quotation marks.
False = 0
True = 1
but,
When
"False" = 0 == string
"True" = 0 == string
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question