How to solve the problem of case insensitivity in MySQL queries

How to solve the problem of case insensitivity in MySQL queries

question

Recently, when I was completing a practical project using the SSH framework, I encountered an inexplicable bug that bothered me for a long time. Finally, I solved it and recorded it as follows.

Question: When testing the system, a student suddenly discovered that the account saved in the database should have been admin, but the student successfully logged in with the Admin account...

...EXM? ? ? Is this ok? Well, I'd better find out the cause of this bug. Then I went through various troubleshooting procedures, but I couldn't find any problems. Finally, I thought of writing SQL statements to query the database directly instead of HQL, and I found the problem:

select * from user where username = 'admin' and password = 'admin';
select * from user where username = 'Admin' and password = 'admin';

Using the above two SQL statements to query the table separately, the results are surprisingly the same! ……! ! Go to the search engine and search for the keywords: MySQL query case, and you will find the problem! MySQL queries are not case sensitive! This really shocked me. Although I knew that keywords are generally not case-sensitive, I didn't expect that even the parameters to be queried are not case-sensitive! ! Try the following SQL statement again, and the result is still the same.

select * from user where username = 'ADMIN' and password = 'admin';

Solution

I searched for a related article on the Internet, which is well written. I will paste the article explanation here:

The default character search strategy of Mysql is: utf8_general_ci, which means case-insensitive; utf8_general_cs means case-sensitive; utf8_bin means binary comparison, which is also case-sensitive. (Note: In MySQL 5.6.10, utf8_genral_cs is not supported! ! ! !)

When creating a table, directly set the collate property of the table to utf8_general_cs or utf8_bin; if the table has already been created, directly modify the Collation property of the field to utf8_general_cs or utf8_bin.

-- Create table:
CREATE TABLE testt(
id INT PRIMARY KEY,
name VARCHAR(32) NOT NULL
)ENGINE = INNODB COLLATE = utf8_bin;
-- Modify the Collation property of the table structure ALTER TABLE TABLENAME MODIFY COLUMN COLUMNNAME VARCHAR(50) BINARY CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL;

Simply modify the SQL statement and add the binary keyword before the field to be queried.

-- Add the binary keyword before each condition select * from user where binary username = 'admin' and binary password = 'admin';
-- Surround the parameters with binary('') select * from user where username like binary('admin') and password like binary('admin');

Note: In my project, the hibernate framework is used, and the hql statement is not sql. The from User where binary username = ? and binary password = ?; results in an error. Then try from User where username like binary(?) and password like binary(?); without an error. The reason is currently unknown.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Notes on MySQL case sensitivity
  • MySQL character types are case sensitive
  • How to set mysql to case insensitive
  • Analysis of problems caused by MySQL case sensitivity
  • MySQL database case sensitivity issue
  • Detailed explanation of MySQL table name case-insensitive configuration method
  • Linux system MySQL forgotten password, reset password, ignore the case of table and column names
  • How to distinguish uppercase and lowercase letters in strings when querying MySQL
  • MySql query case insensitive solution (two)
  • MySQL table name case selection

<<:  Detailed instructions for installing Jenkins on Ubuntu 16.04

>>:  Vue+ssh framework to realize online chat

Recommend

How to dynamically add modules to Nginx

Written in front Often, after we install Nginx ba...

5 ways to quickly remove the blank space of Inline-Block in HTML

The inline-block property value becomes very usef...

MySQL SQL statement analysis and query optimization detailed explanation

How to obtain SQL statements with performance iss...

Implementation of CSS circular hollowing (coupon background image)

This article mainly introduces CSS circular hollo...

Compatibility with the inline-block property

<br />A year ago, there were no articles abo...

JavaScript adds prototype method implementation for built-in objects

The order in which objects call methods: If the m...

JavaScript+HTML to implement student information management system

Table of contents 1. Introduction 2. Rendering 3....

Deep understanding of JavaScript syntax and code structure

Table of contents Overview Functionality and read...

Detailed explanation of CSS pre-compiled languages ​​and their differences

1. What is As a markup language, CSS has a relati...

Detailed explanation of for loop and double for loop in JavaScript

for loop The for loop loops through the elements ...

Pure CSS to achieve input box placeholder animation and input verification

For more exciting content, please visit https://g...

How to implement parent-child component communication with Vue

Table of contents 1. Relationship between parent ...

Detailed explanation of basic concepts of HTML

What is HTML? HTML is a language used to describe...