Home >Backend Development >PHP Tutorial >Regarding the problem of php7 design link mysqlutf8mb4 character set

Regarding the problem of php7 design link mysqlutf8mb4 character set

WBOY
WBOYOriginal
2016-07-06 13:51:301488browse

After installing php7 through epel source, connect to the database and directly prompt:

<code>Unable to set client connection character set: utf8mb4</code>

No error is reported when using utf8

Find a solution: directly use the sql statement to set the character set

<code>SET NAMES 'utf8mb4';</code>

But it always feels bad to use this method, and there are many places that need to be changed in the previous code, which is a bit uneconomical

Hey guys, do you have a solution? Thanks in advance

PS: I couldn’t find a solution after searching on Google for a whole day, but it’s probably a silly problem

================================================== =================

The cause of the problem was found, and it turned out to be just a configuration problem

<code>$pdo = new PDO('mysql:host=localhost;dbname=my_db;charset=utf8mb4', 'db_user', 'db_password');</code>

to:

<code>$pdo = new PDO('mysql:host=localhost;dbname=my_db', 'db_user', 'db_password', array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
));</code>

I don’t know why the new version of the pdo driver cannot directly set the connection character set to utf8mb4. Logically speaking, it should be backward compatible, but at least a solution has been found :-D

Reply content:

After installing php7 through epel source, connect to the database and directly prompt:

<code>Unable to set client connection character set: utf8mb4</code>

No error is reported when using utf8

Find a solution: directly use the sql statement to set the character set

<code>SET NAMES 'utf8mb4';</code>

But it always feels bad to use this method, and there are many places that need to be changed in the previous code, which is a bit uneconomical

Hey guys, do you have a solution? Thanks in advance

PS: I couldn’t find a solution after searching on Google for a whole day, but it’s probably a silly problem

================================================== =================

The cause of the problem was found, and it turned out to be just a configuration problem

<code>$pdo = new PDO('mysql:host=localhost;dbname=my_db;charset=utf8mb4', 'db_user', 'db_password');</code>

to:

<code>$pdo = new PDO('mysql:host=localhost;dbname=my_db', 'db_user', 'db_password', array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
));</code>

I don’t know why the new version of the pdo driver cannot directly set the connection character set to utf8mb4. Logically speaking, it should be backward compatible, but at least a solution has been found :-D

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