Web Development note "Two" PHP garbled problem solving

Source: Internet
Author: User

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

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.