对于PHP+MYSQL的中文乱码问题的理解和困惑
首先先说下现在已调试好的无乱码配置。mySQL的dos的客户端编码配置为(通过set names gbk 语句设置):
character_set_client | gbk
character_set_connection | gbk
character_set_database | utf8
character_set_filesystem | binary
character_set_results | gbk
character_set_server | utf8
character_set_system | utf8
在php文件中设置显示编码为utf-8。通过查询语句获取知,除filesystem外,其它编码方式都是utf-8。由于每次重启mysql或重启mysql命令行客户端时,编码方式都会重置为安装所选择的默认编码方式(本例中采用了utf8编码。在软件编码配置页,默认为latin1,通过选择第三项(manual)配置编码语言。这点对于初学者很重要!),所以每次都需要手动通过set names gbk来设置编码。
接下来再科普下character_set_connection、character_set_results、 character_set_client的含义。通常的使用中,character_set_client,character_set_connection这两个变量的值是一样的,也就是说查询不需要进行编码转换。这样看来变量character_set_connection有些多余。当查询进入时,查询会被服务器从 character_set_client转换到character_set_connection,当查询执行时,查询会被服务器从 character_set_connection转换到列字符集。查询返回时,数据直接被服务器从列字符集转换到 character_set_results。由此可见,这三个参数主要用于客户端与服务器进行交互的过程中编码格式的转换。在进行编码格式设置时,这三个必须保持同步,否则就会出现乱码。character_set_server ,character_set_system这两项是服务器参数,这里暂时不作讨论,默认为utf-8编码格式。
下面说说我的理解和困惑。
问题:当命令行客户端通过set names utf8编码进行中文输入存储时,命令行客户端能正常显示中文,php编写的网页不能正常显示中文。但是网页中选择GB2312编码显示时,中文恢复正常显示。类似的,当命令行客户端通过set names gbk编码进行中文输入存储时,命令行客户端能正常显示中文,php编写的网页可以正常显示中文(网页默认utf-8)。
理解:命令行客户端默认是gbk输入方式。当系统设置为utf8时,通过insert命令对数据库添加数据时,实际上是将gbk强制转换为utf8,然后存储进数据库。php网页仍然认为是utf8码,所以显示乱码。而当网页强制为GB2312码时,相当于又进行了一次转码,所以又显示正常了。而对于命令行客户端,我们实际是通过character_set_results参数控制显示的。当选择utf8码时,则在utf8码设置下的插入的中文显示正常,当选择gbk码时,则在gbk码设置下的插入的中文显示正常。问题的症结点在于,不管mySQL编码怎么设置,当我们通过命令行客户端输入时,它实际输入的内容是gbk码的。这也解释了为什么在设置为utf8码时,只能输入部分中文字符。如,在utf8码下无法输入中文字符串“男孩”。
当然,上述也是我的一些个人理解和猜测。在进一步学习mySQL后,如果发现上述有偏颇或错误。我将会后续进行更正。
此文参考了两处文章,感谢如下两位同学的分享,网址如下:
http://qq695198038.blog.163.com/blog/static/1662751322012329113552221/
http://www.bitsCN.com/database/201108/101151.html
http://www.cnblogs.com/xwdreamer/archive/2012/07/11/2585993.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
![VMware Horizon Client cannot be opened [Fix]](https://img.php.cn/upload/article/000/887/227/170835607042441.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
VMware Horizon Client helps you access virtual desktops conveniently. However, sometimes the virtual desktop infrastructure may experience startup issues. This article discusses the solutions you can take when the VMware Horizon client fails to start successfully. Why won't my VMware Horizon client open? When configuring VDI, if the VMWareHorizon client is not open, an error may occur. Please confirm that your IT administrator has provided the correct URL and credentials. If everything is fine, follow the solutions mentioned in this guide to resolve the issue. Fix VMWareHorizon Client Not Opening If VMW is not opening on your Windows computer
![VMware Horizon client freezes or stalls while connecting [Fix]](https://img.php.cn/upload/article/000/887/227/170942987315391.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When connecting to a VDI using the VMWareHorizon client, we may encounter situations where the application freezes during authentication or the connection blocks. This article will explore this issue and provide ways to resolve this situation. When the VMWareHorizon client experiences freezing or connection issues, there are a few things you can do to resolve the issue. Fix VMWareHorizon client freezes or gets stuck while connecting If VMWareHorizon client freezes or fails to connect on Windows 11/10, do the below mentioned solutions: Check network connection Restart Horizon client Check Horizon server status Clear client cache Fix Ho

Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

Excel spreadsheet is one of the office software that many people are using now. Some users, because their computer is Win11 system, so the English interface is displayed. They want to switch to the Chinese interface, but they don’t know how to operate it. To solve this problem, this issue The editor is here to answer the questions for all users. Let’s take a look at the content shared in today’s software tutorial. Tutorial for switching Excel to Chinese: 1. Enter the software and click the "File" option on the left side of the toolbar at the top of the page. 2. Select "options" from the options given below. 3. After entering the new interface, click the “language” option on the left

How to display Chinese characters correctly in PHPDompdf When using PHPDompdf to generate PDF files, it is a common challenge to encounter the problem of garbled Chinese characters. This is because the font library used by Dompdf by default does not contain Chinese character sets. In order to display Chinese characters correctly, we need to manually set the font of Dompdf and make sure to select a font that supports Chinese characters. Here are some specific steps and code examples to solve this problem: Step 1: Download the Chinese font file First, we need

"WWE2K24" is a racing sports game created by Visual Concepts and was officially released on March 9, 2024. This game has been highly praised, and many players are eagerly interested in whether it will have a Chinese version. Unfortunately, so far, "WWE2K24" has not yet launched a Chinese language version. Will wwe2k24 be in Chinese? Answer: Chinese is not currently supported. The standard version of WWE2K24 in the Steam Chinese region is priced at 199 yuan, the deluxe version is 329 yuan, and the commemorative edition is 395 yuan. The game has relatively high configuration requirements, and there are certain standards in terms of processor, graphics card, or running memory. Official recommended configuration and minimum configuration introduction:

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

Title: An effective way to repair Chinese garbled characters in PHPDompdf. When using PHPDompdf to generate PDF documents, garbled Chinese characters are a common problem. This problem usually stems from the fact that Dompdf does not support Chinese character sets by default, resulting in Chinese content not being displayed correctly. In order to solve this problem, we need to take some effective ways to fix the Chinese garbled problem of PHPDompdf. 1. Use custom font files. An effective way to solve the problem of Chinese garbled characters in Dompdf is to use
