Table of Contents
分析:
以下是部分摘录:
所以说:
修改MySql默认编码
Home Database Mysql Tutorial MySql中SETNAMESutf8详解(乱码原理)_MySQL

MySql中SETNAMESutf8详解(乱码原理)_MySQL

Jun 01, 2016 pm 01:02 PM
Garbled characters principle



不多说,先上图

\

分析:

set names utf8之前,

character_set_client | gbk

character_set_connection| gbk

character_set_results | gbk

set names utf8之后,

character_set_client | utf8

character_set_connection| utf8

character_set_results | utf8

关于字符集可参照mysql中文手册

地址:http://dev.mysql.com/doc/refman/5.1/zh/charset.html#charset-connection

以下是部分摘录:

在客户端和服务器的连接处理中也涉及了字符集和校对规则变量。每一个客户端有一个连接相关的字符集和校对规则变量。

考虑什么是一个“连接”:它是连接服务器时所作的事情。客户端发送SQL语句,例如查询,通过连接发送到服务器。服务器通过连接发送响应给客户端,例如结果集。对于客户端连接,这样会导致一些关于连接的字符集和校对规则的问题,这些问题均能够通过系统变量来解决:

· 当查询离开客户端后,在查询中使用哪种字符集?

服务器使用character_set_client变量作为客户端发送的查询中使用的字符集。

· 服务器接收到查询后应该转换为哪种字符集?

转换时,服务器使用character_set_connection和collation_connection系统变量。它将客户端发送的查询从character_set_client系统变量转换到character_set_connection(除非字符串文字具有象_latin1或_utf8的引介词)。collation_connection对比较文字字符串是重要的。对于列值的字符串比较,它不重要,因为列具有更高的校对规则优先级。

· 服务器发送结果集或返回错误信息到客户端之前应该转换为哪种字符集?

character_set_results变量指示服务器返回查询结果到客户端使用的字符集。包括结果数据,例如列值和结果元数据(如列名)。

你能够调整这些变量的设置,或可以依赖默认值(这样,你可以跳过本章)。

有两个语句影响连接字符集:

SET NAMES 'charset_name'

SET CHARACTERSETcharset_name

SET NAMES 'x'语句与这三个语句等价:

mysql>SETcharacter_set_client = x;

mysql>SETcharacter_set_results = x;

mysql>SETcharacter_set_connection = x;

所以说:

set names utf8指定了客户端和服务器之间传递字符的编码规则为UTF8。

修改MySql默认编码

MySql默认编码是Latin1,不支持中文,要想修改默认编码,需要修改my.ini文件

[client]增加default-character-set=utf8

[mysql]增加default-character-set=utf8

[mysqld]增加

character_set_server=utf8

init_connect='SET NAMES utf8'

cmd下操作mysql乱码需要注意cmd编码

查看当前代码页,如果当前代码页是GBK那么需要set names GBK才可以保证不乱码。

chcp 65001/936修改代码页。完美解决乱码问题。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve Chinese garbled characters in Linux How to solve Chinese garbled characters in Linux Feb 21, 2024 am 10:48 AM

The Linux Chinese garbled problem is a common problem when using Chinese character sets and encodings. Garbled characters may be caused by incorrect file encoding settings, system locale not being installed or set, and terminal display configuration errors, etc. This article will introduce several common workarounds and provide specific code examples. 1. Check the file encoding setting. Use the file command to view the file encoding. Use the file command in the terminal to view the encoding of the file: file-ifilename. If there is "charset" in the output

Methods and detailed analysis to solve the problem of garbled characters in some win11 software Methods and detailed analysis to solve the problem of garbled characters in some win11 software Jan 30, 2024 pm 03:54 PM

Many users found that their personal software was garbled after upgrading the win11 system. So how to solve this problem? Now let the editor carefully introduce to users the analysis of garbled code problems in some software in win11. Analysis of garbled characters in some software in win11 1. Click the search box in the taskbar in the lower left corner and enter control panel to open it. 3. Click on the area. 5. Then uncheck the small box for beta version in the window, and finally restart the computer to solve the problem.

Analysis of the function and principle of nohup Analysis of the function and principle of nohup Mar 25, 2024 pm 03:24 PM

Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

How to solve the problem of garbled characters when importing Chinese data into Oracle? How to solve the problem of garbled characters when importing Chinese data into Oracle? Mar 10, 2024 am 09:54 AM

Title: Methods and code examples to solve the problem of garbled characters when importing Chinese data into Oracle. When importing Chinese data into Oracle database, garbled characters often appear. This may be due to incorrect database character set settings or encoding conversion problems during the import process. . In order to solve this problem, we can take some methods to ensure that the imported Chinese data can be displayed correctly. The following are some solutions and specific code examples: 1. Check the database character set settings In the Oracle database, the character set settings are

Tips for dealing with garbled Chinese file names in PHP Tips for dealing with garbled Chinese file names in PHP Feb 27, 2024 pm 02:18 PM

Tips for dealing with garbled Chinese file names in PHP During the development process, we often encounter the problem of garbled Chinese file names, especially when processing files uploaded by users. In PHP, how to correctly handle garbled file names is a common and important problem. This article will introduce some techniques for dealing with garbled Chinese file names and provide specific code examples to help readers better deal with this challenge. Problem description: When users upload files, the Chinese file names sometimes appear to be garbled. This is because different operating systems and browsers

How to solve the problem of garbled characters displayed on Win11 when booting? Two solutions to the garbled characters displayed on Win11 boot How to solve the problem of garbled characters displayed on Win11 when booting? Two solutions to the garbled characters displayed on Win11 boot Feb 29, 2024 pm 12:16 PM

Win11 is Microsoft's latest operating system, but some users may encounter the problem of displaying garbled characters when booting, which will affect the normal use of the system. This article will introduce some methods to solve this problem. Method 1: 1. Press the [Win+S] key combination, or click the [Search icon] next to the start icon on the taskbar. In the opened Windows search, enter [Control Panel] in the search box, and then click [Open]. The best matching control panel application out of the control panel window; 2. In the control panel window, switch to the [Category] view mode, and then click [Clock and Zone-Region]; 3. In the zone window, switch to the [Management] tab, and then click [Change] System Regional Settings]; 4. [Uncheck] Beta version: Use Unicode

How to deal with garbled characters in Linux terminal How to deal with garbled characters in Linux terminal Mar 20, 2024 pm 03:12 PM

How to deal with the problem of garbled characters in the Linux terminal. When using the Linux system, sometimes the text displayed in the terminal will be garbled. This brings inconvenience to us when using the terminal and needs to be dealt with in time. This article will introduce how to deal with some common Linux terminal garbled problems, and provide specific code examples. Problem 1: Garbled Chinese characters on the terminal. Garbled Chinese characters on the terminal are usually caused by incorrect character encoding settings on the terminal. We can solve this problem by modifying the terminal's character encoding settings. #View the current terminal

In-depth discussion of the principles and practices of the Struts framework In-depth discussion of the principles and practices of the Struts framework Feb 18, 2024 pm 06:10 PM

Principle analysis and practical exploration of the Struts framework. As a commonly used MVC framework in JavaWeb development, the Struts framework has good design patterns and scalability and is widely used in enterprise-level application development. This article will analyze the principles of the Struts framework and explore it with actual code examples to help readers better understand and apply the framework. 1. Analysis of the principles of the Struts framework 1. MVC architecture The Struts framework is based on MVC (Model-View-Con

See all articles