PDO in PHP to solve the problem of Chinese garbled code

  • 2020-03-31 21:17:14
  • OfStack

My environment is the appsver package,

The most common online solution to the Chinese garbled code is:

Type 1: PDO:: arbitration ($DSN, $user, $pass, array

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

I tried the first method, but it turned out that the name field only displayed a single 'C' character. The space that should show Chinese after is blank.

The result is as shown in figure 1
< img border = 0 SRC = "http://files.jb51.net/upload/201009/20100906122630405.jpg" border = 0 >
I only want to solve: directly replace UTF8 with GBK, it is ok, namely:

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

NAMES 'GBK';" ));

Effect picture 2 is as follows:
< img border = 0 SRC = "http://files.jb51.net/upload/201009/20100906122703148.jpg" border = 0 >

Type 2: PDO:: s/s ($DSN, $user, $pass);

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

The second one, which I also tested in my environment, displays as shown in figure 1. In this case, replace utf8 with GBK to display

Shown. In addition, the PDO here :: is used when using $PDO -> Instead, of course, this is a variable, and the variable name can be defined by itself.

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

As for the third kind, after reading the above two kinds, I should also know to replace utf8 with GBK, which can also be displayed correctly.

I've tested all of them. Will do. Ha ha. In addition, here I also introduce a way to solve the Chinese garbled code, but more or less the same,

It is basically nothing but different from the third method, except that it USES exec instead of query. The code is as follows:

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

Ha ha. Well, I've tried all four myself.

Related articles: