0

Can anyone point out this behaviour and hw to stop this ?

I know about casting or adding double quotes.

I was wondering if there is any other way

Heres table definition

CREATE TABLE `countries` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `country_name` varchar(100) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1

enter image description here

The record with id 1 is correct but id 2 doesn't satisfy the where condition but is returned.

4
  • Is there any TABLE Definition changes that can fix this ? Commented Jul 27, 2018 at 10:20
  • 1
    No there isn't you have to code for it. Commented Jul 27, 2018 at 11:57
  • Tell doctrine that country_name you search for is a string, and don't hand over an integer Commented Jul 27, 2018 at 12:02
  • Undeniably, it is a strange name for a country Commented Jul 27, 2018 at 15:46

2 Answers 2

2

Try this:

select * from countries where country_name='7884'
Sign up to request clarification or add additional context in comments.

1 Comment

Correct, like I said I know this is the fix. But I'm using Laravel (Doctrine) and the driver is firing the query.. So I was wondering of a structure wise solution, that way I won't have to make changes in every file / conditions with such case.
1

The problem is, when you write country_name = 7884, that country_name is implicitly casted as a signed integer. And in the casting process the 'S' is simply ignored, as you can see here. That selects the second row because cast('1884S' AS signed) = 1884 = 1884 (your input).

As fa06 already suggested, surround the operand by single quotes to make it a string and prevent the implicit cast -- ... where country_name = '7884'.

1 Comment

Any configuration at mysql settings level to prevent from this auto casting ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.