MySQL and SET NAMES 'utf8'

In MySQL, a database is created in this way CREATE DATABASE db_name DEFAULT CHARACTER SET 'utf8';
When do I need to perform SET NAMES 'utf8' and why do I need it?

Author: jisecayeyo, 2016-08-20

3 answers

SET NAMES utf8 used when your server is not configured to accept data in your encoding (in this case, UTF8).

Then it needs to be explicitly told that you will be sending data in UTF8 encoding and no additional transcoding of data from latin1 to UTF8 is required (by default, it expects data in latin1).

In order to set UTF8 on the server for encoding the connection instead of latin1, you should set the directive character_set_connection{[5 in the my. cnf section [mysqld] ]}

[mysqld]
...
character_set_connection=utf8
 3
Author: cheops, 2016-08-20 11:26:53
SET NAMES 'utf8'

Sets the encoding of the connection , i.e. the encoding in which the client communicates with the server.

This is necessary because the standard encoding of the connection in your system may well be latin1 - even worse, when it stops matching after the script transfer or database update.

The mysql server will convert the data itself if the table contains data in a different encoding. True, it is it can lead to errors if, for example, you write to the table

CREATE TABLE `t` (`a` VARCHAR(255) NULL DEFAULT NULL) COLLATE='cp1251_general_ci' ENGINE=InnoDB;

The character , which is not in the table encoding.

The tables in the database can be in any encoding. individual fields in the table can also have their own encoding.

 2
Author: Sanya_Zol, 2016-08-20 12:12:49

Guys, there is a MySQL manual that says that SET NAMES is equivalent to these three commands:

SET character_set_client = charset_name;
SET character_set_results = charset_name;
SET character_set_connection = charset_name;
 0
Author: strangeqargo, 2016-08-20 12:09:41