Some Supplements to the PDO solution for Chinese garbled characters in PHP

Source: Internet
Author: User

My environment is a Failover ver package,

The code that most frequently appears on the Internet to solve Chinese Garbled text display is:

First: PDO ::__ construct ($ dsn, $ user, $ pass, array

(PDO: MYSQL_ATTR_INIT_COMMAND => "set names 'utf8 ';"));

I tried the first method. The result is that the name field only displays one 'C' character. After that, the Chinese characters are displayed blank.

The result is as follows: 1 shows

I only need to solve this problem: replace UTF8 with GBK directly, that is:

PDO: :__ construct ($ dsn, $ user, $ pass, array (PDO: MYSQL_ATTR_INIT_COMMAND => "SET

NAMES 'gbk ';"));

2 is as follows:


Type 2: PDO ::__ construct ($ dsn, $ user, $ pass );

PDO: exec ("set names 'utf8 ';");

The second type is also tested in my environment. The display effect 1 shows. In this case, replace utf8 with GBK to display

. In addition, Here PDO: $ pdo-> is used in place. Of course, this is a variable, and the variable name can be customized.

Third: $ pdo-> query ('set names utf8 ;');

As for the third type, I have read the above two types. You should also know that you should replace utf8 with GBK, which can also be correctly displayed.

I have tested these methods. Yes. Haha. In addition, I will introduce a method to solve Chinese Garbled text here, but it is similar,

There is nothing to do with the third method, but it doesn't work. This method uses exec instead of query. The Code is as follows:

$ Pdo-> exec ("set character set gbk ");

Haha. I have tried these four methods myself.

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.