PHP MySQL garbled problem, using set NAMES UTF8 correction _php Tips

Source: Internet
Author: User
Tags php mysql
Write it down before you can find it again!
When PHP operates the database, the data in the database uses UTF8 encoding, and when it is read out, it shows all the??????? The question mark garbled, looked for some data originally is before reads carries on the encoding setting:
Copy Code code as follows:

CREATE TABLE TableName
(
ID int NOT NULL auto_increment,
Title varchar NOT NULL,
Contnet varchar () defalut NULL,
Primary KEY (' ID ')
) Begin=myisam DEFAULT CHARSET =utf8;

To execute before inserting data:
Copy Code code as follows:

mysql_query ("SET NAMES UTF8");

And then
mysql_query ("INSERT INTO TableName ...")

Execute before reading data:
Copy Code code as follows:

mysql_query ("SET NAMES UTF8");

Then mysql_query ("SELECT * FROM TableName")
Note: The code read here is the original encoded content to be encoded after the output, such as the output of the page is GBK encoding, then read out in the page display is garbled, so before the query execution mysql_query ("SET NAMES GBK"), GBK encoded text can be displayed correctly on the page.
Related Article

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.