PHPMYSQL garbled problem, use SETNAMESutf8 to correct _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
If PHPMYSQL is garbled, use SETNAMESutf8 for correction. Write it down first, so that you won't be able to remember it again! When using PHP to operate the database, the data in the database is UTF-8 encoded. when reading the data, all the information displayed is ??????? The question mark is garbled. if you find something, write it down first, so that you won't be able to think about it again!
When using PHP to operate the database, the data in the database is UTF-8 encoded. when reading the data, all the information displayed is ??????? The question mark is garbled. I found some information and made a encoding setting before reading it:

The code is as follows:


Create table tablename
(
Id int not null auto_increment,
Title varchar (20) not null,
Contnet varchar (300) defalut null,
Primary key ('id ')
) Begin = MyISAM default charset = UTF8;


Run the following command before inserting data:

The code is as follows:


Mysql_query ("set names utf8 ");


Then
Mysql_query ("insert into tablename .....")

Run the following command before reading data:

The code is as follows:


Mysql_query ("set names utf8 ");


Then mysql_query ("select * from tablename ")
Note: the encoding read here is output after the original encoding content is re-encoded. for example, if the output content is located on the GBK encoding page, garbled characters are displayed on the page, therefore, execute mysql_query ("set names gbk") before the query to display the GBK-encoded text content on the page.

Success! When using PHP to operate the database, the data in the database is UTF-8 encoded. when reading the data, all the information displayed is ??????? Garbled question marks, found some...

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.