本文主要是介绍解决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>
其中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>
请注意在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 中文乱码问题,可以安心使用啰。

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

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.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

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


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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.