Home  >  Article  >  Database  >  Detailed explanation of the difference between full-width characters and half-width characters stored in MySQL

Detailed explanation of the difference between full-width characters and half-width characters stored in MySQL

黄舟
黄舟Original
2017-05-28 09:55:223043browse

This article mainly introduces MySQLRelated information on the difference between storing full-width characters and half-width characters. Friends in need can refer to it

Unfortunately, when the company was testing IM internally, it When encountering a MYSQL_DATA_TRUNCATED error, the log record still appears when mysql_stmt_fetch is called. In view of previous experience, it should be caused by the insufficient length of the binding area of ​​the given result set. I checked it many times and found no problem. I have corresponding relationships in my code, such as char(20). In my code, I will define char buffer[20]array for storage. It looks so right and perfect. I had no choice but to print each row of data, find the row of records where the error was reported, and found that the font was different from the others. For example:

123456789 (half-width)
123456789(full-width)

This is not controlled by my input of spaces, but full-width and half-width input method. For full-width characters, it is twice the length of half-width characters, and my MySQL uses utf-8, so in the database that is a full-width character that is 3 bytes long.

select length(column) from table_name where…;

You can judge the length by printing this. At this time, you have to consider the problem of matching the length of the type in the code and the length of the database field, such as full-width characters or Chinese characters. Char(20) in the database represents 20 characters, not 20 bytes. Please note here that when fetching data, do not use all 20 as the result set length to fetch data.

The above is the detailed content of Detailed explanation of the difference between full-width characters and half-width characters stored in MySQL. For more information, please follow other related articles on the PHP Chinese website!

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