Home Database Mysql Tutorial MySQL 乱码解决的两大方案介绍

MySQL 乱码解决的两大方案介绍

Jun 07, 2016 pm 04:12 PM
jsp mysql Garbled characters introduce plan correct solve

对正确解决JSP中的乱码问题(解决tomcat下中文乱码问题 )中我们也对MySQL乱码问题进行了详细说明,相信你看完之后会对JSP里的一些乱码问题有所解决,不过还是有些人的MySQL乱码问题没有得到解决。 包括我自己,所以又找了一些资料,希望这次能完全解决MySQL

对正确解决JSP中的乱码问题(解决tomcat下中文乱码问题 )中我们也对MySQL乱码问题进行了详细说明,相信你看完之后会对JSP里的一些乱码问题有所解决,不过还是有些人的MySQL乱码问题没有得到解决。

包括我自己,所以又找了一些资料,希望这次能完全解决MySQL数据库的乱码问题。
 

MySQL 乱码解决方案的第一种方法,很精辟的总结:

经常更换虚拟主机,而各个服务商的MySQL版本不同,当导入数据后,总会出现乱码等无法正常显示的问题,查了好多资料,总结出自己的一点技巧:

WINDOWS 下导入应该这样

使用MySQL的命令

在DOS命令下进入MySQL的bin目录下,输入MySQL -uroot -p密码 数据库名称

MySQL -uroot -p密码 test2以前的国外主机用的MySQL是4.x系列的,感觉还比较好,都无论GBK和UTF-8都没有乱码,没想到新的主机的MySQL是5.0版本的,导入数据后,用Php读出来全是问号,乱码一片,记得我以前也曾经有过一次切换出现乱码的经验,原因肯定是MySQL版本之间的差异问题。

只好查资料,发现了一个解决方法,就是在MySQL_connect后面加一句SET NAMES UTF8,即可使得UTF8的数据库消除乱码,对于GBK的数据库则使用SET NAMES GBK,代码如下:

1

2

3

4

5

6

<ol class="dp-xml">

<li class="alt">

<span><span>$</span><span style="color: white; background-color: navy"><span class="attribute"><span id="1274078464389S" style="display: none"> </span></span></span></span><span ><span class="attribute">mysql</span></span><span><span class="attribute"><span id="1274078465293E" style="display: none"> </span>_mylink</span><span> = </span><span class="attribute-value">mysql_connect</span><span>($mysql_host$mysql_user$mysql_pass);   </span></span>

</li>

<li><span>mysql_query("SET NAMES 'GBK'"); </span></li>

</ol>

Copy after login

数据库字符集为utf-8

连接语句用这个

1

2

3

4

5

6

7

<ol class="dp-xml">

<li class="alt"><span><span>mysql_query("SET NAMES 'UTF8'");   </span></span></li>

<li><span>mysql_query("SET CHARACTER SET UTF8");   </span></li>

<li class="alt">

<span>mysql_query("SET </span><span class="attribute">CHARACTER_SET_RESULTS</span><span>=</span><span class="attribute-value">UTF8</span><span>'"); </span>

</li>

</ol>

Copy after login

还有个方法就是,如果你自己的机器的话,可以更改MYSQL字符集,一般在MYSQ4和MYSQL5这两个版本中有效

mysql 乱码解决方案的第二个方法:很不错的解说,可以试一下

mysql乱码处理总结:

(1)java中处理中文字符正常,在cmd client中显示乱码是字符集的问题.

(2)字段长度设置够长,但插入中文字符时提示com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column错误是字符集的问题.

(3)乱码问题归根到底是字符集的问题,那就从字符集设置方面考虑,不外乎下面几个方面:server,client,database,connection,results.

解决办法:

(1)修改 my.ini(MySQL Server Instance Configuration 文件)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<ol class="dp-xml">

<li class="alt"><span><span># CLIENT SECTION  </span></span></li>

<li><span>[client]  </span></li>

<li class="alt">

<span class="attribute">port</span><span>=</span><span class="attribute-value">3306</span><span> </span>

</li>

<li><span>[mysql]  </span></li>

<li class="alt">

<span class="attribute">default-character-set</span><span>=</span><span class="attribute-value">gbk</span><span> </span>

</li>

<li><span># SERVER SECTION  </span></li>

<li class="alt"><span>[mysqld]  </span></li>

<li>

<span class="attribute">default-character-set</span><span>=</span><span class="attribute-value">gbk</span><span>   </span>

</li>

</ol>

Copy after login

(2)修改data目录中相应数据库目录下的db.opt配置文件

1

2

3

4

5

6

<ol class="dp-xml">

<li class="alt"><span><span class="attribute">default-character-set</span><span>=</span><span class="attribute-value">gbk</span><span> </span></span></li>

<li>

<span class="attribute">default-collation</span><span>=</span><span class="attribute-value">gbk_chinese_ci</span><span> </span>

</li>

</ol>

Copy after login

(3)数据库连接串中指定字符集

URL=jdbc:mysql://yourIP/college?user=root&password=yourPassword&useUnicode=true&characterEncoding=gbk

注:在mysql4.1.0版 要加useUnicode=true&characterEncoding=gbk 如果是5.0版以上的,加不加都是没有关系的!

(4)在创建数据库时指定字符集

1

<ol class="dp-xml"><li class="alt"><span><span>create database yourDB CHARACTER SET gbk; </span></span></li></ol>

Copy after login

(5)在dos命令上要设置

1

<ol class="dp-xml"><li class="alt"><span><span>set names gbk </span></span></li></ol>

Copy after login

好了,经过检查上面四个方面的设置,应该 OK 了!不会出现诸如com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column等问题.

以上的相关内容就是对mysql 乱码解决方案的介绍,望你能有所收获。


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 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Detailed introduction of Samsung S24ai functions Detailed introduction of Samsung S24ai functions Jun 24, 2024 am 11:18 AM

2024 is the first year of AI mobile phones. More and more mobile phones integrate multiple AI functions. Empowered by AI smart technology, our mobile phones can be used more efficiently and conveniently. Recently, the Galaxy S24 series released at the beginning of the year has once again improved its generative AI experience. Let’s take a look at the detailed function introduction below. 1. Generative AI deeply empowers Samsung Galaxy S24 series, which is empowered by Galaxy AI and brings many intelligent applications. These functions are deeply integrated with Samsung One UI6.1, allowing users to have a convenient intelligent experience at any time, significantly improving the performance of mobile phones. Efficiency and convenience of use. The instant search function pioneered by the Galaxy S24 series is one of the highlights. Users only need to press and hold

See all articles