Home mysql MySQL and SET NAMES 'utf8'

MySQL and SET NAMES ‘utf8’




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 character_set_connection

directive in the [mysqld] section in my.cnf section

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;

the character , 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.

Answer 3

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;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions