Home >Backend Development >PHP Tutorial >Some supplements to solve the Chinese garbled problem with PDO in PHP_PHP Tutorial
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
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 :
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.