mysql database scrambling to save Vietnamese scrambling solution
- 2020-06-07 05:24:39
- OfStack
I test 1 myself, many characters become '? '.
The utf8 character set has been used for database connection:
define("MYSQL_ENCODE", "UTF8");
mysql_query('SET NAMES '.MYSQL_ENCODE,$conn) or die(' Character set error '.mysql_error());
For the better part of an hour, I couldn't fix it.
insert's data is still confused, suddenly thought, is the field can not save these Vietnamese.
When I look at the database character set 1, the default is gbk, which is not a problem for Chinese. When I meet the Vietnamese character set 1, the code will be confused.
Therefore, I modified the character set of the fields that might use Vietnamese to utf8 and proofread it to utf8_unicode_ci. (Here 1 must modify a specific field, modify the whole table character set, does not work, the field still retains the original character set) problem solved.
My database is php_college_web
Table: city_article
Fields in Vietnamese: title, content
So sql is implemented as follows:
ALTER TABLE city_article CHANGE title title VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci
ALTER TABLE city_article CHANGE content content text CHARACTER SET utf8 COLLATE utf8_unicode_ci