Home Database Mysql Tutorial 解决MySQL中文乱码的方法归纳

解决MySQL中文乱码的方法归纳

Jun 07, 2016 pm 04:11 PM
mysql Chinese Garbled characters introduce method solve

本文主要是介绍解决MySQL中文乱码的方法归纳,如果你在解决MySQL中文乱码的实际操作中存在不解之处时,你不妨浏览下面的文章,希望你能从中获得自己想要的东西。自从写完下面两篇文章: wordpress 1.5升级注意事项MySQL 中文乱码篇 MySQL(和PHP搭配之最佳组

本文主要是介绍解决MySQL中文乱码的方法归纳,如果你在解决MySQL中文乱码的实际操作中存在不解之处时,你不妨浏览下面的文章,希望你能从中获得自己想要的东西。自从写完下面两篇文章:

wordpress 1.5升级注意事项MySQL 中文乱码篇

MySQL(和PHP搭配之最佳组合) 4.1中文乱码第二击

过去几个月就不断有来信询问各种MySQL(和PHP搭配之最佳组合)中文相容的问题,所以有机会看到许多玩家们用的各式连接法与程式,实在太开眼界。

最近因为转换到flex 2做为开发平台并重写一些产品,许多去年写的framework就无法使用,而其中有部份专门处理多国语系的library也随之失效,导致我得重新面对这个问题,所以正好籍此机会将一些新心得整理出来。

MySQL(和PHP搭配之最佳组合)中文乱码的原因

MySQL(和PHP搭配之最佳组合)会出现MySQL 中文乱码的原因不外乎下列几点:

MySQL(和PHP搭配之最佳组合) server本身设定问题,例如还停留在latin1

MySQL(和PHP搭配之最佳组合) table的语系设定问题(包含character与collation)

客户端程式(例如php)的连线语系设定问题

在之前的两篇文章中已介绍过如何设定MySQL(和PHP搭配之最佳组合) server/table的character/collation。

接下来只需要补充几点注意事:

MySQL(和PHP搭配之最佳组合)MySQL 中文乱码必胜解决法

1、MySQL(和PHP搭配之最佳组合)启动时会读取一个预设的config档,一般名称为my.ini,而它会到下列两位置去寻找这个档案:

C:\windows\my.ini也就是作业系统的安装目录,也有可能是C:\winnt\my.ini

C:\:my.cnf也就是C disk的根目录

注意不同位置里的档案suffix不一样,这点在之前的文章中没有特别强调,因此那时我是用更复杂的方法直接重新将MySQL(和PHP搭配之最佳组合)注册到service里面,并在那里指定my.ini的位置。

2、my.ini里的内容为:

<ol class="dp-xml">
<li class="alt"><span>[MySQL(和PHP搭配之最佳组合)d]  </span></li>
<li>
<span class="attribute">defaultcharacterset</span><span>=</span><span class="attribute-value">utf8</span><span> </span>
</li>
<li class="alt"><span>[client]  </span></li>
<li>
<span class="attribute">defaultcharacterset</span><span>=</span><span class="attribute-value">utf8</span><span> </span>
</li>
<li class="alt">
<span class="attribute">init_connect</span><span>=</span><span class="attribute-value">'SET NAMES utf8'</span><span> </span>
</li>
</ol>
Copy after login

其中MySQL(和PHP搭配之最佳组合)d是指定server启动时要用的语系,但如果这里设定为utf8可能会让许多英文软体不开心,例如osCommerce/mediaWiki,所以这里建议设成latin1。

下面的client则是设定当client连线时要使用什么语系,但可惜的是这个设定不是每个client都会鸟它,基本上只有MySQL(和PHP搭配之最佳组合)自已家的程式,例如MySQL(和PHP搭配之最佳组合).exe, MySQL(和PHP搭配之最佳组合)d.exe, MySQL(和PHP搭配之最佳组合)admin.exe或是MySQL(和PHP搭配之最佳组合) Control Center这种程式会去读取这个设定档然后改用utf8连线。

note:感谢b6s桑热情相助提供第二行指令,据说比一支支php程式去设定connection setting更快,这样应该也可以顺便解决phpmyadmin无法正确显示unicode中文的问题(不过amfphp到是不吃这套,一定要乖乖的自已设定语系才行)

但大部份工程师应该都是自已写php/jsp(SUN企业级应用的首选)程式去连线,此时自然不会读取这个设定而继续使用预设的语系 latin1。

这也正是八成来信朋友阵亡的地方。

通常我会用一个独立的档案来处理MySQL(和PHP搭配之最佳组合) connection的设定,例如:

<ol class="dp-xml">
<li class="alt"><span><span>PLAIN TEXT  </span></span></li>
<li>
<span class="tag"></span><span> </span>
</li>
<li class="alt"><span>//database connection details.  </span></li>
<li>
<span>$</span><span class="attribute">host</span><span> = </span><span class="attribute-value">"localhost"</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">link</span><span> = </span>MySQL<span>(和PHP搭配之最佳组合)_connect($host, "xxx", "xxx");  </span>
</li>
<li>MySQL<span>(和PHP搭配之最佳组合)_query("SET NAMES 'utf8'");  </span>
</li>
<li class="alt">MySQL<span>(和PHP搭配之最佳组合)_select_db("your_table_name_here", $link);  </span>
</li>
<li>
<span class="tag">?></span><span>  </span>
</li>
</ol>
Copy after login

请注意在MySQL(和PHP搭配之最佳组合)_connect后第五行的地方有加上一个"set names 'utf8' "的指令,告诉MySQL(和PHP搭配之最佳组合)这个connection之后的连线内容都要使用utf8,经过这样设定后,通常就可以解决大部份问题。

从这里也可以推想得知,如果你有用某种custom connection pooling机制,就要记得在每次建立新的connection后立即设定为utf8。

这组设定经过几天的反覆测试(使用中文繁、简、日文、韩文)后已经证实完全不会有乱码或是某些字变成"口"。

例子:试者输入这几个字「不道可否啰」如果它们能正确进入MySQL(和PHP搭配之最佳组合)再被读出来,那就是中文没问题了,如果读出来后变成『口』字那就是革命还未成功同志仍需努力...orz

当然我也顺便验证了flex2 amfphp php MySQL(和PHP搭配之最佳组合)来回传resultset也不再会有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

Video Face Swap

Video Face Swap

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

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)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

See all articles