Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql string conversion return 0

Tags:

database

mysql

why select 'aaa' =0 return 1 (TRUE) if i have a table like

userid | pass

user1  | pas1

if I query :

select from table where userid = 0 and pass =0 

it gives me all the rows?

like image 762
amd Avatar asked Mar 30 '12 18:03

amd


People also ask

What is cast () in MySQL?

The MySQL CAST() function is used for converting a value from one datatype to another specific datatype. The CAST() function accepts two parameters which are the value to be converted and the datatype to which the value needs to be converted.

How do I get the first letter of a string in MySQL?

MySQLi For Beginners To fetch the first alphabet from the strings, use LEFT(). This method allows you to return characters from the left of the string.

How do I find a character in a string MySQL?

MySQL LOCATE() Function The LOCATE() function returns the position of the first occurrence of a substring in a string. If the substring is not found within the original string, this function returns 0. This function performs a case-insensitive search.


1 Answers

MySQL sees 'aaa' = 0 and thinks to itself:

"I can either convert aaa to an integer, or 0 to a string."

Guess which one it goes with?

Basically what's happening is that 'aaa' is being converting to an integer, and as it's not a valid integer, it casts to 0.

0 = 0 is of course true (or true == 1).

I suspect the same is happening with your userid column, though without knowing its values/datatype, it's hard to say.

http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html

like image 128
Corbin Avatar answered Sep 19 '22 00:09

Corbin