Home Database Mysql Tutorial 对MySQL 中文乱码的解决方案的描述

对MySQL 中文乱码的解决方案的描述

Jun 07, 2016 pm 04:09 PM
mysql Chinese main Garbled characters article yes solution

以下的文章主要是对MySQL 中文乱码解决方案的描述,MySQL 中文乱码在实际的应用中是经常出现的在实际的运行中带来很多的不便,以下的文章就是介绍MySQL 中文乱码解决方案。 过去几个月就不断有来信询问各种MySQL(和PHP搭配之最佳组合)中文相容的问题,所以有

以下的文章主要是对MySQL 中文乱码解决方案的描述,MySQL 中文乱码在实际的应用中是经常出现的在实际的运行中带来很多的不便,以下的文章就是介绍MySQL 中文乱码解决方案。

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

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

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

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

-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里的内容为:

[MySQL(和PHP搭配之最佳组合)d]

<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>
<li><span>[client]    </span></li>
<li class="alt">
<span class="attribute">default-character-set</span><span>=</span><span class="attribute-value">utf8</span><span>   </span>
</li>
<li>
<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>
</ol>
Copy after login

$link = MySQL(和PHP搭配之最佳组合)_connect($host, "xxx", "xxx");

MySQL(和PHP搭配之最佳组合)_query("SET NAMES 'utf8'");

MySQL(和PHP搭配之最佳组合)_select_db("your_table_name_here", $link);

?>

请注意在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

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 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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.

Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Jun 03, 2024 pm 01:25 PM

Common challenges faced by machine learning algorithms in C++ include memory management, multi-threading, performance optimization, and maintainability. Solutions include using smart pointers, modern threading libraries, SIMD instructions and third-party libraries, as well as following coding style guidelines and using automation tools. Practical cases show how to use the Eigen library to implement linear regression algorithms, effectively manage memory and use high-performance matrix operations.

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 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 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 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

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.

Java framework security vulnerability analysis and solutions Java framework security vulnerability analysis and solutions Jun 04, 2024 pm 06:34 PM

Analysis of Java framework security vulnerabilities shows that XSS, SQL injection and SSRF are common vulnerabilities. Solutions include: using security framework versions, input validation, output encoding, preventing SQL injection, using CSRF protection, disabling unnecessary features, setting security headers. In actual cases, the ApacheStruts2OGNL injection vulnerability can be solved by updating the framework version and using the OGNL expression checking tool.

See all articles