PHP garbled problem is mainly due to the Chinese coding problem, the Chinese encoding is generally used to gbk,utf8 mostly.
In order to make the Chinese encoding no longer garbled, the solution is as follows:
1. Add the header to PHP ("content-type:text/html; Charset=utf-8 ") This sentence must begin with the first sentence after the <?php tag in PHP.
That
<? PHP Header ("content-type:text/html; Charset=utf-8 "); // ?>
2. Set the database connection to UTF-8 encoding, before manipulating the database, declaring
<? PHP Header ("content-type:text/html; Charset=utf-8 "); // Code mysql_query ("Set names ' UTF8 '"); // database Operation ....? >
3. Database encoding set to Utf-8 (MySQL as an example)
Ways to query the character set:
We do this when we change the database character set: 1. Modify permissions for the database configuration file (you can modify My.ini):
For example: Locate the following configuration in the My.ini file:
Default-character-set = UTF8
Character_set_server = UTF8
2. My.ini configuration permissions are not modified (typically on a cloud host)
This can be set in Phpadmin:
or execute the SQL command:
mysql> SET character_set_client = UTF8;
mysql> SET character_set_connection = UTF8;
mysql> SET character_set_database = UTF8;
mysql> SET character_set_results = UTF8;
mysql> SET character_set_server = UTF8;
mysql> SET collation_connection = UTF8;
mysql> SET collation_database = UTF8;
mysql> SET collation_server = UTF8;
Finally, if the above operation, that is: The database encoding is UTF8, the data table encoding is UTF8, the field of the data is encoded UTF8,
This is still garbled because of the problem may occur when the database connection (the database connection may require external parameters, the encoding of this parameter is also important)
So we're going to set this up:
SET NAMES ' UTF8 ';
and add the following sentence before the database executes: it works the same as:
Web Development note "Two" PHP garbled problem solving