The example in this article describes the Chinese sorting method of UTF8 encoding in PHP and MYSQL, and is shared with everyone for your reference. The specific implementation method is as follows:
Generally speaking, there are three sorting methods in Chinese:
1. Sort according to pinyin;
2. Sort according to strokes;
3. Sort according to radicals.
The default sorting method of the system is pinyin sorting, which is also commonly used by us. The following is sorting by pinyin
1. It is necessary to sort in Chinese in the php array, but files in utf8 format are generally used, and direct sorting with asort will not work. You can use gbk and gb2312. This has something to do with the encoding of several formats. The coding of gbk and gb2312 itself is sorted by pinyin.
2. In MySQL, we often sort and query a field, but when sorting and searching in Chinese, the sorting and search results of Chinese characters are often 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 that MySQL is not case-sensitive when querying strings. When compiling MySQL, the ISO-8859 character set is generally used as the default character set. Therefore, the case conversion of Chinese coded characters occurs during the comparison process. this phenomenon.
Solution:
Add the "binary" attribute to the field containing Chinese characters to make it a binary comparison, for example, change "name char(10)" to "name char(10)binary".
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 (the default is latin1). You can also use extra-charsets=gb2312,gbk to add multiple character sets.
If you do not want to modify the table structure or recompile MySQL, you can also use the CONVERT function in the order by part of the query statement. For example
I hope this article will be helpful to everyone’s PHP+MySQL programming.