Php files all use UTF-8 encoding, and the database also changes to utf8 encoding. The text output in the browser is normal, but the database shows Chinese garbled characters. how can this problem be solved? Php files all use UTF-8 encoding, and the database also changes to utf8 encoding. The text output in the browser is normal, but the database shows Chinese garbled characters. how can this problem be solved?
Reply content:
Php files all use UTF-8 encoding, and the database also changes to utf8 encoding. The text output in the browser is normal, but the database shows Chinese garbled characters. how can this problem be solved?
Mysql> show variables like 'character % ';
+ --------- + ---------- +
| Variable_name | Value |
+ --------- + ---------- +
| Character_set_client | utf8 |
| Character_set_connection | utf8 |
| Character_set_database | utf8 |
| Character_set_filesystem | binary |
| Character_set_results | utf8 |
| Character_set_server | utf8 |
| Character_set_system | utf8 |
| Character_sets_dir |/usr/share/mysql/charsets/|
+ --------- + ---------- +
If there is a change to my. ini or my. cnf that is not utf8
Add in [client]
Default-character-set = utf8
Add in [mysqld]
Character-set-server = utf8
Init_connect = 'set NAMES utf8'
Add in [mysql]
Default-character-set = utf8
Restart mysql
If it needs to take effect temporarily, set field = utf8 for example
Set character_set_client = utf8
I have never used an old-fashioned one... you can see the following two types.
If you use mysqli:
mysqli::set_charset ( "utf8" );
If PDO is used, (recommended ):
$pdo = new PDO("mysql:host=localhost;dbname=test;charset=utf8", 'username', 'password');
mysql_query("SET NAMES 'UTF8'");
Try ....
You only find Chinese garbled characters in the database. There are two steps to solve this problem.
1. set names UTF-8;
In the select statement, check whether it is normal. if it is abnormal, check the second step.
2. if you use secret CRT to connect to the server for viewing, set the currently configured character set. [Optional]
Set the encoding of mysql database and PHP file to the same encoding format at the same time. generally, UTF-8 is used.