query('set names utf8;');" method; 3. Use the exec method to solve it."/> query('set names utf8;');" method; 3. Use the exec method to solve it.">

Home  >  Article  >  Backend Development  >  How to solve php pdo garbled code

How to solve php pdo garbled code

藏色散人
藏色散人Original
2020-07-07 11:16:373005browse

php pdo garbled solution: 1. Set the statement "PDO::exec("SET NAMES 'utf8';");"; 2. Use "$pdo->query('set names utf8 ;');" method; 3. Use the exec method to solve the problem.

How to solve php pdo garbled code

PDO in PHP solves Chinese garbled characters

The code that appears most frequently on the Internet to solve Chinese garbled characters is:

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

(PDO::MYSQL_ATTR_INIT_COMMAND => “SET NAMES’utf8′;”));

I tried the first method, but the result is that the name field Only one 'C' character is displayed. The subsequent place where Chinese characters should be displayed is blank.

The result is this: As shown in the picture


I just want Solution: Just replace UTF8 with GBK, that is:

PDO::__construct($dsn, $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => “SET NAMES’GBK’;”));

The rendering 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. When encountering this situation, put Replace utf8 with GBK and it will be displayed. In addition, the 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 method :$pdo->query('set names utf8;');

As for the third type, after reading the above two, you should know that you need to replace utf8 with GBK, and it can be displayed correctly.

I have tested all of these. They all work. Haha. In addition, I will also introduce a method to solve Chinese garbled characters here, but they are similar.

Basically and the third There is nothing wrong with this method. The only problem is that this method does not use query but exec. The code is as follows:

$pdo->exec(“SET CHARACTER SET GBK”);

The above methods have been tested and can solve the problem of Chinese garbled characters.

For more related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to solve php pdo garbled code. For more information, please follow other related articles on the PHP Chinese website!

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