0

Can anyone please help me to understand the MySQL select query behavior and help me to solve this,

How to show no results found message based on user search?

Table Structure:

CREATE TABLE `dummytable` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `mobile` int(11) NOT NULL,
  `welcome` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1

enter image description here

Question: Why I am getting this result?

SELECT * FROM `dummytable` WHERE `mobile` = '\'\'' LIMIT 50


enter image description here

1
  • if we add WHERE mobile = 'okay' is also results the same. Anyone can help to solve this Commented Aug 3, 2016 at 11:30

3 Answers 3

3

The column mobile is declared as an integer. When MySQL compares an integer to a string, then it converts the string to a number -- not the other way around.

How does MySQL do this? It converts the leading numeric characters to a number. Your condition is:

WHERE mobile = ''''

(The traditional SQL way to put a single quote in a string is to double it up.)

The single quote is not a numeric character, so the conversion results in 0. And hence, that is the result that you get.

Sign up to request clarification or add additional context in comments.

2 Comments

how to solve this.. If people are searching okay on input box then the result become invalid. How to solve this, In this case I have to display no results found.
Test the input value outside the database. If you are coming from php, do the test there.
1

After execution of this query MySQL shows you a warning message:

mysql> select * from foo where bar_integer='\'\''

....

10 rows in set, 1 warning (0.01 sec)

mysql> show warnings;
+---------+------+------------------------------------------+
| Level   | Code | Message                                  |
+---------+------+------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'okay' |
+---------+------+------------------------------------------+

MySQL tries to convert you string value to destination type (integer), if convertion fails mysql interprites it as 0 which results to what you see.

If you turn on MySQL strict mode this query would produce an error instead of making this implicit cast.

2 Comments

how to solve this.. If people are searching okay on input box then the result become invalid. How to solve this, In this case I have to display no results found
You should validate user input on client side before querying mysql. It's not a mysql problem.
1

Your column mobil is an integer column. So everything is converted to int. An empty string is converted to 0 (and any other string not starting with a number will also be converted to zero I think)

1 Comment

yes if we add WHERE mobile = 'okay' is also results the same

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.