Comparing a string with a number field that returns unexpected results

Based on the table:

CREATE TABLE IF NOT EXISTS `users` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 

Add a couple of lines:

 INSERT INTO `users` (`id`,`name`) VALUES (NULL , 'Bob'),(NULL , 'Larry'),(NULL , 'Steve'); 

Why, OH WHY! Does this query return results:

 SELECT * FROM `users` WHERE id = "2this-is-not a numeric value" 

Result:

 query returned 1 row(s) in 0.0003 sec id name ----------------- 2 Larry 

The string used in the where clause is explicitly converted to a numeric value - who told you to do this ?! I cannot find any documentation that suggests that mysql or PHP can use the automatic listing of my string literal.

This only works if the numeric character is the first in the string, "this 2 is not numeric" will not return results. "12 2" will become 12 , "1 2" (one-space-two) will become 1 .

Any articles or documents explaining this behavior will be appreciated.

+6
source share
1 answer

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


All Articles