For PHP operations on MYSQL databases and garbled issues, let's take a look and help solve them.

Source: Internet
Author: User
For PHP operations on the MYSQL database, garbled characters can be found and solved as follows: my webpage is GB2312 encoded and my database code is UTF8, if I want to add mysql_query (& quot; setnamesgb2312 & quot;) to read the contents of the database on the webpage, I can solve the garbled problem about PHP operations on the MYSQL database, the problem of garbled code can be solved by experts.
My situation is as follows:
My webpage is GB2312 encoded, and my database code is UTF8. if I want to read the content in the database to the webpage, I need to add mysql_query ("set names gb2312 ") this statement can solve the problem of garbled text, so that the information displayed on the webpage is not garbled.



However, when I add a statement on the webpage to insert the database content, if I do not add mysql_query ("set names gb2312"), the content of the inserted database is garbled, because the database encoding is UTF8 and the content on the webpage is GB2312, it is garbled.

However, after mysql_query ("set names gb2312") is added, the content added to the database will not be garbled.

What I don't understand is that mysql_query ("set names gb2312") can solve the problem of inserting Chinese characters into the UTF8 database to solve Chinese character garbled characters?

Mysql_query ("set names gb2312") is it true that the character set is set to GB2312? Why can't I insert garbled characters into the UTF8 database? On the contrary, if I do not add this sentence, it will show garbled characters?

Experts Help me

------ Solution --------------------
Set names is used to set the character set of the client. it has nothing to do with the server,
Set names gb2312 and so on. before insertion, I told the database that it was in gb2312 encoding. the database will process it according to the encoding when it comes in. if this sentence is not added, the database does not know what encoding it comes in, so garbled text occurs.

In the same way, set names gb2312 is used to tell the database that I want to get the gb2312 encoding data. you can return the gb2312 encoding data for me, and the database will give the gb2312 data, if this parameter is not added, UTF-8 is displayed. garbled characters are displayed on the webpage.

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.