Home Database Mysql Tutorial MySQL 中文显示乱码

MySQL 中文显示乱码

Jun 07, 2016 pm 03:34 PM
mysql Chinese Garbled characters about show

最近关于中文显示乱码的贴子比较多,所以也做了个总结: 可以参考一下杨涛涛版主的《各种乱码问题汇总》 http://topic.csdn.net/u/20071124/08/3b7eae69-ed1d-4a77-8895-9930bf3601af.html MySQL字符集的原理介绍。摘录于官方文档。http://dev.mysql.com/doc

最近关于中文显示乱码的贴子比较多,所以也做了个总结:

可以参考一下杨涛涛版主的《各种乱码问题汇总》
http://topic.csdn.net/u/20071124/08/3b7eae69-ed1d-4a77-8895-9930bf3601af.html

MySQL字符集的原理介绍。摘录于官方文档。http://dev.mysql.com/doc/refman/5.1/zh/charset.html

不同的编码格式会导致同一字符,在不同字符集下的编码会不同。同样同一编码在不同的字符集中代码的字符也不相同。当你的MySQL返回的字符串的编码格式(字符集)与你的客户工具程序(mysql, php, query browser, ...)当前使用的字符集不同时,就会造成乱码。 比如一个英国朋友告诉你Long, 当一位中国小学生看到后就会告诉你“龙”而不是“长”

关于字符集的详细介绍和例子,建议花一点时间看一下
http://dev.mysql.com/doc/refman/5.1/zh/charset.html  (第10章:字符集支持)。


这里仅摘要一下。

MySQL中默认字符集的设置有四级:服务器级,数据库级,表级 。最终是字段级 的字符集设置。注意前三种均为默认设置,并不代码你的字段最终会使用这个字符集设置。所以我们建议要用show create table table ; 或show full fields from tableName; 来检查当前表中字段的字符集设置。

MySQL中关于连接环境的字符集设置有  Client端,connection, results 通过这些参数,MySQL就知道你的客户端工具用的是什么字符集,结果集应该是什么字符集。这样MySQL就会做必要的翻译,一旦这些参数有误,自然会导致字符串在转输过程中的转换错误。基本上99%的乱码由些造成。

乱码后需要检查的信息。(如果需要论坛上的朋友帮助,建议你提供以下信息 )


1. 数据库表中字段的字符集设置show create table TableName show full columns from tableName


mysql> show create table t1;
+-------+------------------------------------
| Table | Create Table                      
+-------+------------------------------------
| t1    | CREATE TABLE `t1` (
  `id` int(11) NOT NULL,
  `c1` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`)   
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+------------------------------------
1 row in set (0.00 sec)
                       
mysql> show full columns from t1;
+-------+-------------+----------------+------+-----+-
| Field | Type        | Collation      | Null | Key |
+-------+-------------+----------------+------+-----+-
| id    | int(11)     | NULL           | NO   | PRI |
| c1    | varchar(30) | gbk_chinese_ci | YES  |     |
+-------+-------------+----------------+------+-----+-
2 rows in set (0.00 sec)

mysql>

 

2. 当前联接系统参数  show variables like 'char%'


mysql> show variables like 'char%';
+--------------------------+----------------
| Variable_name            | Value
+--------------------------+----------------
| character_set_client     | gbk
| character_set_connection | gbk
| character_set_database   | latin1
| character_set_filesystem | binary
| character_set_results    | gbk
| character_set_server     | latin1
| character_set_system     | utf8
| character_sets_dir       | C:/Program File
+--------------------------+----------------
8 rows in set (0.00 sec)

mysql>


1. 中文,请确保 表中该字段的字符集为中文兼容:
 big5     | Big5 Traditional Chinese
 gb2312   | GB2312 Simplified Chinese
 gbk      | GBK Simplified Chinese
 utf8     | UTF-8 Unicode

 

2. 确保,联接参数与这个字段字符集一致,你可以用 set name 'charsetname';
 比如, set name 'gbk';
 这条命令会同时修改 character_set_client,character_set_connection,character_set_results
 (如果你的这架MySQL中都为中文,则你可以在my.ini或my.cnf中加上或修改这个参数, 参数文件修改后需重启MySQL服务)
[mysql]
default-character-set=gbk

 

3. PHP 乱码, 同样 mysql_query("set name 'gbk'"); 其它API也类似。

 

4. phpmyadmin里乱码
phpMyAdmin的config.inc.php中有没有设置$cfg['DefaultCharset']='utf-8';

 

5. Windows操作系统中命令行("DOS"窗口)下。
 在你的DOS窗中的左上角标题栏片左键,属性,
 在字体中,选择“宋体”,确认
 mysql中 set names 'gbk';

 

6. ADO.NET, ADO中 ,可以连接字符串中加入CharSet=UTF8;类似指令以说明connection的字符集。
 Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; CharSet=UTF8;

 

7. SQL Manager for MySQL

用EMS建数据库,

 Character Set设为utf-8

 client charset设UTF-8

 Font charset 设为GB2312_CHARSET


8. jdbcodbc桥接 http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/bridge.html

       // Load the JDBC-ODBC bridge driver
       Class.forName(sun.jdbc.odbc.JdbcOdbcDriver) ;

      
// setup the properties
       java.util.Properties prop = new java.util.Properties();
       prop.put(
" charSet " , " Big5 " );
       prop.put(
" user " , username);
       prop.put(
" password " , password);

      
// Connect to the database
       con = DriverManager.getConnection(url, prop);

 

9.  PHP 5.2 版本以上解决乱码问题的一个方法 (由 ljf_ljf [Mark Liang] 提供)

    $conn = mysql_connect ( " 192.168.1.133 " , " root " , " 123456 " ) or
       
die ( " Could not connect: " . mysql_error ());

   
$program_char = " utf8 " ;

   
$conn . mysql_select_db ( " test " );
   
// $conn.mysql_query('SET @@character_set_results = "'.$program_char.'"');
   
    mysql_set_charset(
$program_char , $conn );
   
$charset = mysql_client_encoding ( $conn );
   
printf ( " current character set is %s
" , $charset );
   
$result = mysql_query ( " SELECT id, task_no,pack_path FROM tb_workplan where id = 1 " , $conn );
   
while ( $row = mysql_fetch_array ( $result , MYSQL_BOTH)) {
       
printf ( " ID: %s
task_no: %s 
pack_path :%s
" , $row [ " id " ] , $row [ 1 ] , $row [ " pack_path " ]); 
    }
   
$conn . mysql_free_result ( $result );
   
$conn . mysql_close ();

 

9.  存储过程参数乱码

create procedure t ( aa char(10) charset 'gbk')

未完。。。

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks 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)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

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: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

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.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

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.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

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.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

What software is better for yi framework? Recommended software for yi framework What software is better for yi framework? Recommended software for yi framework Apr 18, 2025 pm 11:03 PM

Abstract of the first paragraph of the article: When choosing software to develop Yi framework applications, multiple factors need to be considered. While native mobile application development tools such as XCode and Android Studio can provide strong control and flexibility, cross-platform frameworks such as React Native and Flutter are becoming increasingly popular with the benefits of being able to deploy to multiple platforms at once. For developers new to mobile development, low-code or no-code platforms such as AppSheet and Glide can quickly and easily build applications. Additionally, cloud service providers such as AWS Amplify and Firebase provide comprehensive tools

See all articles