query('set names utf8;');" method; 3. Use the exec method to solve it."> How to solve php pdo garbled code-PHP Problem-php.cn

How to solve php pdo garbled code

藏色散人
Release: 2023-03-02 07:12:02
Original
2994 people have browsed it

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′;”));
Copy after login

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’;”));
Copy after login

The rendering is as follows:


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

PDO::exec(“SET NAMES ‘utf8’;”);
Copy after login

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”);
Copy after login

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

For more related knowledge, please visitPHP 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!

Related labels:
pdo
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!