Home  >  Article  >  Backend Development  >  Some supplements to solve the Chinese garbled problem with PDO in PHP_PHP Tutorial

Some supplements to solve the Chinese garbled problem with PDO in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:34:28821browse

My environment is the appsver package.

The most common code to solve Chinese garbled display on the Internet is:

The first one: PDO::__construct($dsn, $user , $pass, array

(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES'utf8';"));

I tried the first method, but the result was Yes, the name field only displays a 'C' character. After that, the place where Chinese should be displayed is blank.

The result is like this: As shown in Figure 1
Some supplements to solve the Chinese garbled problem with PDO in PHP_PHP Tutorial
I only need to solve it: directly replace UTF8 with GBK, that is:

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

NAMES'GBK';"));

Rendering 2 is as follows :
Some supplements to solve the Chinese garbled problem with PDO in PHP_PHP Tutorial

Second type: PDO::__construct($dsn, $user, $pass);

PDO::exec(" SET NAMES 'utf8';");

I have also tested the second method in my environment, and the display effect is as shown in Figure 1. In this case, replace utf8 with GBK, and it will Shown

. In addition, PDO:: here is replaced by $pdo-> when using it. Of course, this is a variable, and the variable name can be defined by yourself.

The third type: $pdo->query('set names utf8;');

As for the third type, after reading the above two, it should be I also know that if I replace utf8 with GBK, it will be displayed correctly.

I have tested all of these. Either way. Ha ha. In addition, I also introduce here a method to solve Chinese garbled characters, but it is similar.

It is basically the same as the third method but different. The strange thing is that this method does not use query but exec. , the code is as follows:

$pdo->exec("SET CHARACTER SET GBK");

Haha. How about it? I have tried all four methods myself.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322408.htmlTechArticleMy environment is the appsver package. The code that often appears on the Internet to solve Chinese garbled display is: The first type :PDO::__construct($dsn, $user, $pass, array (PDO::MYSQL_ATTR_INIT_COMMAND = "...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn