Php + mysql garbled problem guidance

Source: Internet
Author: User
Php + mysql garbled problem guidance database environment: redhat AS5 + mysql5.0.4 UTF8 encoding
WEB environment: UTF8 encoding for the win2003 nginx1.5 php5.2.17 page.
Other instructions: an application (purchased) uses the mysql5.0.4 Database. I now use php to write a website to display part of the content in the database. one field stores the name ,, garbled characters are displayed on the page, and the same garbled code is displayed when I use mysql management tools. I used to write this website using ASP. NET (C #) write, at that time is to try a variety of encoding, and finally determine the ISO8599-1 encoding to UTF8 can be normally displayed. At that time, the method to handle garbled text was to use a function for transcoding. the reading was converted to UTF8. Currently, PHP is not familiar with it and there is no solution.
The following is the code (C #) I used to solve this garbled problem. I don't know how to change it to PHP,
// Read is to convert private string ISO8599ToUTF8 (string str) {Encoding UTF_8 = System. text. encoding. getEncoding ("UTF-8"); Encoding ISO = System. text. encoding. getEncoding ("ISO8859-1"); byte [] iso = ISO. getBytes (str); return UTF_8.GetString (iso );}

Reply to discussion (solution)

You can retrieve the fields of garbled fields from the database, for example, save them to $ s.
Then echo base64_encode ($ s); paste the result

Thank you for your reply. what you see is that the result "********************************** will certainly not be base64-encoded.

Assume that your garbled content is in $ s
Echo base64_encode ($ s );
Post result

Yiyi AngelIs that the answer?

The hexadecimal internal code of the garbled text you posted is efbbbfc395c39bc392c3adc38cc3acc38ac2b9.
Efbbbf is the BOM header of the UTF-8 character set.
Apparently, you executed the set names utf8 command before reading data.
C395c39bc392c3adc38cc3acc38ac2b9 is the result of converting latin1 to UTF-8 encoding.
Latin1 (general European character set 1) is iso 8859-1
After iconv ('utf-8', 'latin1', substr ($ s, 3 ));
After transcoding, The Hex Angel's hexadecimal internal code is d5dbd2edcceccab9.
It is obviously a gbk encoded string

Thank you!

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: 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.