In MySQL, the database is created in this way
CREATE DATABASE db_name DEFAULT CHARACTER SET 'utf8';
When do you need to
SET NAMES 'utf8' and what is it for?
Answer 1, authority 100%
SET NAMES utf8 is used when your server is not configured to receive data in your encoding (in this case, UTF8).
Then he needs to be explicitly told that you will send data in UTF8 encoding and no additional data conversion from latin1 to UTF8 is required (by default it expects data in latin1).
To set UTF8 instead of latin1 on the server for encoding the connection, set the
directive in the
[mysqld] section in my.cnf section
[mysqld] ... character_set_connection = utf8
Answer 2, authority 67%
SET NAMES 'utf8'
sets the encoding of the connection , i.e. the encoding in which the client communicates with the server .
This must be done because the standard encoding of connection in your system may well be
latin1 – even worse, when it ceases to match after a script transfer or database update.
The mysql server will convert the data itself if the table contains data in a different encoding. True, this can lead to errors if, for example, writing to a table
CREATE TABLE `t` (` a` VARCHAR (255) NULL DEFAULT NULL) COLLATE = 'cp1251_general_ci' ENGINE = InnoDB;
▲ , which is not in the table encoding.
tables in the database can be in any encoding. individual fields in the table can also have their own encoding.
guys, there is a MySql manual that says
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;