False value in Where Where Returns all rows?

I have a query that looks like this:

`SELECT id, username FROM table_name WHERE username=0` 

When I run this query, MySQL returns all rows in table_name. Also, if I replace 0 with false, I get the same results. If I use null or empty string, I do not get returned strings (as expected).

The username column is varchar (50), if that matters.

Then my question is ::

Why is adding 0 or false to this query returns all the rows in the table? Is this a MySQL setup?

This bothers me a bit, since I worked on the assumption that the above request will not return rows (in this particular case), and I am wondering if this happens elsewhere in my application and what unforeseen consequences it may have.

+6
source share
1 answer

The username is passed to int of 0 to do the mapping. 0 = 0 is true, so there is no WHERE clause here. You can use username='' if you are trying to get a non-zero empty username.

+5
source

Source: https://habr.com/ru/post/888828/


All Articles