About the pitfalls of implementing specified encoding in MySQL

About the pitfalls of implementing specified encoding in MySQL

Written in front

Environment: MySQL 5.7+, MySQL database character encoding is utf8, table is also utf8

Scenario: WeChat authorizes the acquisition of user information (including nickname) and saves it to the database. Some users succeed, but a few fail.

So why did it fail?

Paste the error log

Incorrect string value: '\xF0\x9F\x98\x98\xF0\x9F...' for column 'nickname' at row 1

This is because the failed user's WeChat nickname contains emoji symbols.

Analysis:

MySQL supports multiple character encoding sets, such as UTF-8, GB2312, GBK, etc.

You can view it through the SHOW CHARSET command.

We usually recommend using UTF-8 as the default character encoding.

From the above figure we can see that the MySQL database has two sets of UTF-8 encoding implementations.

  • utf8: utf8 encoding only supports up to three bytes. In utf8 encoding, Chinese characters take up 3 bytes, and other English characters, numbers, and symbols all take up 1 byte. However, emoji symbols take up 4 bytes, and some more complex text and traditional Chinese characters also take up 4 bytes.
  • utf8mb4: A complete implementation of the UTF-8 encoding. It supports up to 4 bytes, so it can be used to store emoji symbols.

Check the information, official explanation:

at last

Tips: If you have a field that needs to store emoji symbols, remember to specify the encoding as utf8mb4.

This is the end of this article about a small pit in the implementation of MySQL specified encoding. For more relevant MySQL specified encoding content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of MYSQL stored procedure comments
  • In-depth analysis of MySQL index data structure
  • Essential conditional query statements for MySQL database
  • Example of how to configure the MySQL database timeout setting
  • Detailed explanation of the installation, configuration, startup and shutdown methods of the Mysql server

<<:  An example of using Dapr to simplify microservices from scratch

>>:  Detailed explanation of compatibility issues and solutions for setting max-width and min-width in table

Recommend

8 JS reduce usage examples and reduce operation methods

reduce method is an array iteration method. Unlik...

Play mp3 or flash player code on the web page

Copy code The code is as follows: <object id=&...

Introduction to the use of base link tag base

<br />When you click the link, the web page ...

Docker custom network implementation

Table of contents 1. Customize the network to rea...

A brief analysis of the differences between undo, redo and binlog in MySQL

Table of contents Preface 【undo log】 【redo log】 【...

HTML basic summary recommendation (text format)

HTML text formatting tags 標簽 描述 <b> 定義粗體文本 ...

Analysis of implicit bug in concurrent replication of MySQL 5.7

Preface Most of our MySQL online environments use...

Collection of 25 fonts used in famous website logos

This article collects the fonts used in the logos...

How to create a my.ini file in the MySQL 5.7.19 installation directory

In the previous article, I introduced the detaile...

How to change the domestic source of Ubuntu 20.04 apt

UPD 2020.2.26 Currently Ubuntu 20.04 LTS has not ...

Vue+el-table realizes merging cells

This article example shares the specific code of ...

HTML drawing user registration page

This article shares the specific implementation c...

Three solutions for sub-functions accessing external variables in JavaScript

Preface When we write web pages, we will definite...