Home  >  Article  >  Backend Development  >  Why does my Mysql not support Chinese query_PHP tutorial

Why does my Mysql not support Chinese query_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:04:26809browse

Q:
My problem when writing a query condition is as follows:
For example, I want to write all the records containing the word "李" in a field
$str="李";
select * from table where field like '%$str%' ;
In addition to records containing the word "李", there are also records that do not contain the word "李". Why?
A:
In MySQL, when sorting and searching Chinese characters, the sorting and search results for Chinese characters are wrong. This situation exists in many versions of MySQL. If this problem is not solved, then MySQL will not be able to actually handle Chinese.
The reason for this problem is: MySQL is case-insensitive when querying strings. When compiling MySQL, the ISO-8859 character set is generally used as the default character set. Therefore, during the comparison process, the Chinese coded character size Write conversion causes this behavior.
Method 1:
The solution is to add the "binary" attribute to the field containing Chinese to make it a binary comparison, for example, change "name char(10)" to "name char(10)binary".
Method 2:
If you use source code to compile MySQL, you can use the --with--charset=gbk parameter when compiling MySQL, so that MySQL will directly support Chinese search and sorting.
Method 3:
You can use Mysql’s locate function to determine. Taking the above problem as an example, the usage method is:
SELECT * FROM table WHERE locate(field,'李') > 0;
This site uses this method, and it feels pretty good. :P
Method 4:
Change your Select statement to this, SELECT * FROM TABLE WHERE FIELDS LIKE BINARY '%FIND%'!


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445210.htmlTechArticleQ: The questions I have when writing a query condition are as follows: For example, I want to write a field that contains the word Li All records$str=李; select * from table where field like '%$str%'; Displayed records...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn