Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PDO example - SET NAMES utf8mb4 #734

Closed
odan opened this issue Feb 28, 2017 · 3 comments
Closed

Update PDO example - SET NAMES utf8mb4 #734

odan opened this issue Feb 28, 2017 · 3 comments

Comments

@odan
Copy link

odan commented Feb 28, 2017

SET NAMES indicates what character set the client will use to send SQL statements to the server. Thus, SET NAMES 'utf8mb4' tells the server, “future incoming messages from this client are in character set utf8mb4. It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a SELECT statement.)

https://dev.mysql.com/doc/refman/5.7/en/charset-connection.html

I would suggest to update the PDO example like this:

$link = new PDO(
    'mysql:host=your-hostname;dbname=your-db;charset=utf8mb4',
    'your-username',
    'your-password',
    array(
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_PERSISTENT => false,
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci"
    )
);

What do you think?

Edit: Added COLLATE utf8mb4_unicode_ci

@jaylinski
Copy link

Laravel is also using this command:

https://github.com/laravel/framework/blob/v5.4.14/src/Illuminate/Database/Connectors/MySqlConnector.php#L56

By default they do it like this: set names utf8mb4 collate utf8mb4_unicode_ci.

@odan odan closed this as completed Mar 3, 2018
@aarie33
Copy link

aarie33 commented Aug 11, 2020

I am so helped with this answer
I was confused with this issue, I tried many ways to fix it.
But finally, my actual problem is about the syntax, this syntax is the best that I can use

Thank you

@tgvishal
Copy link

'mysql:host=your-hostname;dbname=your-db;charset=utf8mb4',
This worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants