Php and mysql garbled characters-php Tutorial

Source: Internet
Author: User
Tags php and mysql
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.