MySQL中文问题的解决方案
以下的文章主要讲述的是MySQL中文问题的实际解决方案,我们大家都知道MySQL中文问题的编码设置与乱码问题一直是困扰计算机人员的心病,可以说对MySQL中文问题的编码设置与乱码问题的解决也是迫在眉睫。 MySQL 4.1的字符集支持(Character Set Support)有两个
以下的文章主要讲述的是MySQL中文问题的实际解决方案,我们大家都知道MySQL中文问题的编码设置与乱码问题一直是困扰计算机人员的心病,可以说对MySQL中文问题的编码设置与乱码问题的解决也是迫在眉睫。
MySQL 4.1的字符集支持(Character Set Support)有两个方面:字符集(Character set)和排序方式(Collation)。对于字符集的支持细化到四个层次: 服务器(server),数据库(database),数据表(table)和连接(connection)。我们最终的目标是使得这四个层次转化会支持中文的编码,下面以utf8为例。
1. 首先查看系统的字符集和排序方式。如果想查看某个特定的数据库的字符集和排序方式,应该先选定数据库,MySQL>use databasename;
<ol class="dp-xml"> <li class="alt">MySQL<span><span class="tag">></span><span> SHOW VARIABLES LIKE ''character_set_%''; </span></span> </li> <li><span>+--------------------------+----------------------------+ </span></li> <li class="alt"><span>| Variable_name | Value | </span></li> <li><span>+--------------------------+----------------------------+ </span></li> <li class="alt"><span>| character_set_client | latin1 | </span></li> <li><span>| character_set_connection | latin1 | </span></li> <li class="alt"><span>| character_set_database | latin1 | </span></li> <li><span>| character_set_results | latin1 | </span></li> <li class="alt"><span>| character_set_server | latin1 | </span></li> <li><span>| character_set_system | utf8 | </span></li> <li class="alt"> <span>| character_sets_dir | /usr/share/</span>MySQL<span>/charsets/ | </span> </li> <li><span>+--------------------------+----------------------------+ </span></li> </ol>
2 rows in set (0.00 sec)
<ol class="dp-xml"> <li class="alt">MySQL<span><span class="tag">></span><span> SHOW VARIABLES LIKE ''collation_%''; </span></span> </li> <li><span>+----------------------+-------------------+ </span></li> <li class="alt"><span>| Variable_name | Value | </span></li> <li><span>+----------------------+-------------------+ </span></li> <li class="alt"><span>| collation_connection | latin1_swedish_ci | </span></li> <li><span>| collation_database | latin1_swedish_ci | </span></li> <li class="alt"><span>| collation_server | latin1_swedish_ci | </span></li> <li><span>+----------------------+-------------------+ </span></li> </ol>
3 rows in set (0.00 sec) 上面列出的值就是系统的默认值,lanti是不支持中文的,所以我们把它改成urf8。
下面我们就来修改这四个层次的编码方式:
1 在my.cf文件的[MySQL中文问题]段设置:
<ol class="dp-xml"><li class="alt"><span><span class="attribute">default-character-set</span><span>=</span><span class="attribute-value">utf8</span><span> </span></span></li></ol>
这条语句的作用是把character_set_client, character_set_connection_,character_set_results 设定为utf8,包括对应的排列方式的编码。 Character_set_server是系统编码,这个是不用改的。
当然改变character_set_client, character_set_connection_,character_set_results编码方式的另外一种方法是:
<ol class="dp-xml"><li class="alt"><span><span>SET NAMES “UTF8”; </span></span></li></ol>
它的作用相当于:
<ol class="dp-xml"> <li class="alt"><span><span>SET </span><span class="attribute">character_set_client</span><span> = </span><span class="attribute-value">utf8</span><span>; </span></span></li> <li> <span>SET </span><span class="attribute">character_set_results</span><span> = </span><span class="attribute-value">utf8</span><span>; </span> </li> <li class="alt"> <span>SET </span><span class="attribute">character_set_connection</span><span> = </span><span class="attribute-value">utf8</span><span>; </span> </li> </ol>
2. 改变数据库的编码方式
<ol class="dp-xml"><li class="alt"><span><span>alter database databasename character set utf8; </span></span></li></ol>
通过以上的更改数据库的编码工作算是昨晚了,当然除了这个方法还有一个万能个的方法,那就是看着那个不是utf8,你只要把相应的编码改成utf8就可以了。格式如下:SET
<ol class="dp-xml"><li class="alt"><span><span class="attribute">character_set_client</span><span> = </span><span class="attribute-value">utf8</span><span>; </span></span></li></ol>
3. 不管你是web编程还是桌面编程,你的数据库的连接地址url中,一定要后缀:
?useUnicode=true&characterEncoding=utf-8/hibernate?useUnicode=true&characterEncoding=utf-8
当然如果你采用了MySQL中文问题的管理工具如MySQL yog,MySQL manager那么你数据库设定也可以在可视化环境下直接设置。当然这也只是限制在database的设定。
【编辑推荐】

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.
