Table of Contents
回复讨论(解决方案)

mysql乱码

Jun 23, 2016 pm 01:37 PM

我把mysql里的my.ini的编码都设置成utf-8

[client]port=3306default-character-set=utf8[mysql]default-character-set=utf8[mysqld]#绑定IPv4和3306端口bind-address = 127.0.0.1port = 3306default-character-set=utf8# 设置mysql的安装目录basedir=D:/MySQL/mysql-5.6.21-win32# 设置mysql数据库的数据的存放目录datadir=D:/MySQL/mysql-5.6.21-win32/data# 允许最大连接数max_connections=200
Copy after login


但显示出来的不是utf-8 重启后也没效果


我在网上试了好几种方法都解决不了乱码的问题,求高人指教


回复讨论(解决方案)

你要用命令去设置mysql的字符集,你只是修改了本地的配置文件并不能达到你想要的效果;还有就是修改完成后记得重启mysql服务。

你要用命令去设置mysql的字符集,你只是修改了本地的配置文件并不能达到你想要的效果;还有就是修改完成后记得重启mysql服务。



命令的我修改完,只是修改的那次有效,下次打开又还原了

在[mysqld]节添再加如下两句试一下
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'

没事去改my.ini 中的参数干什么?

乱码出现在哪? 你是怎么插入的?

在[mysqld]节添再加如下两句试一下
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'



修改完,重启后没有效果

mysql 启动时并无默认加载文件
如果你想通过 my.ini 改变 mysql 的默认行为,需在 MySQL启动时加上
--defaults-file=my.ini
注意:my.ini 是全路径的


数据库不是为你一个人服务的,所以应该是你去适应环境,而不是环境来适应你

mysql 启动时并无默认加载文件
如果你想通过 my.ini 改变 mysql 的默认行为,需在 MySQL启动时加上
--defaults-file=my.ini
注意:my.ini 是全路径的


数据库不是为你一个人服务的,所以应该是你去适应环境,而不是环境来适应你



defaults-file=my.ini要添加在哪个地方?

没事去改my.ini 中的参数干什么?

乱码出现在哪? 你是怎么插入的?



乱码出现在数据库的表中 字段的值 中文全是乱码;

你怎么启动 mysql 的?


问题就出在这,怎样让root获取super权限,没有super权限,就只能Read Only

这权限好头疼~

root用户, 搜索grant privileges

呃,编码问题怎么又扯到,Read Only的问题了.
编码无外乎就是一致性的问题,你可以读一下mysql的手册关于编码的说明。

就像status输出的 Server Db Client Conn 分别有编码设置。并且你建库建表的时候还可以指定不同的编码设置,你以另外一个连接连上来的时候也可以设置另外不同的编码来和服务器交互。

看你的status输出还显示的是 latin1 和 gbk ,那说明你的mysql程序并没有读取这个my.ini
至少我设置了
[client]
port=3306
default-character-set=utf8

[mysql]
default-character-set=utf8
之后
client和conn两项都是utf8的了

你执行 mysql.exe --help 里面有提示mysql.exe查找配置文件的顺序, 你看你的配置文件的位置是否正确,或者有高优先级的在它之前被读取了。

还有楼上有同学批评我和你改 server 的配置,我是看到你 root@localhost 觉得你应该是自己笔记本上测试开发用本地db server才让你改的。如果数据库是线上用的,是不要随便改这个东西。要想清楚再改。

还有有空可以读一下手册对应章节
https://dev.mysql.com/doc/refman/5.6/en/charset.html
在以后的日子里告别所有与此相关的问题。

我现在把数据库,表,字段都设置成utf8,项目编码也设置成utf8,还是会出现乱码.
我在mysql里执行插入中文字符语句时,不会出现乱码.将查询语句搬到项目里,插入的却是乱码.
用 mysql_client_encoding($con) 返回的是 latin1
我在连接是加上 mysql_set_charset( utf8, $conn ),返回的还是 latin1

show variables like 'char%';character_set_client	utf8character_set_connection	utf8character_set_database	latin1character_set_filesystem	binarycharacter_set_results	utf8character_set_server	latin1character_set_system	utf8
Copy after login

我试着用 set name 'utf8';将character_set_database,character_set_server, character_set_filesystem改完后,下次又还原了
my.ini
[client]port=3306default-character-set=utf8[mysql]default-character-set=utf8[mysqld]#绑定IPv4和3306端口bind-address = 127.0.0.1port = 3306default-character-set=utf8
Copy after login

改完后重启好像一点效果都没有.

还是那话:数据库不是为你一个人服务的,所以应该是你去适应环境,而不是环境来适应你!
所以不要去改动 MySQL 的默认设置去适应你的要求
如果你一定要这样做(或是作为学习),应从安装 MySQL 开始
在 MySQL 的安装过程中有一个扩展选项,用来设置 MySQL 的默认字符集
安装完之后,你再去观察正确的字符集组合设置是怎样的

在使用 MySQL 时,是不能改变 MySQL 的默认设置的(因为你的改动可能会影响到几个、几十个甚至几百个人的工作)
你能改变的只是专属于你的库、表、字段的字符集设置,而且是一旦改了,就不要再动了。否则会直接影响到你自己在不同时期的工作

原则是:
含有中文的字段应设置为宽字符集(gbk、utf8、ucs-2...)任何一种都可以
在连接 MySQL 后执行 set name XXX 指令(XXX 为你程序文件所使用字符集),通知 MySQL 以何种字符集与你会话

还是那话:数据库不是为你一个人服务的,所以应该是你去适应环境,而不是环境来适应你!
所以不要去改动 MySQL 的默认设置去适应你的要求
如果你一定要这样做(或是作为学习),应从安装 MySQL 开始
在 MySQL 的安装过程中有一个扩展选项,用来设置 MySQL 的默认字符集
安装完之后,你再去观察正确的字符集组合设置是怎样的

在使用 MySQL 时,是不能改变 MySQL 的默认设置的(因为你的改动可能会影响到几个、几十个甚至几百个人的工作)
你能改变的只是专属于你的库、表、字段的字符集设置,而且是一旦改了,就不要再动了。否则会直接影响到你自己在不同时期的工作

原则是:
含有中文的字段应设置为宽字符集(gbk、utf8、ucs-2...)任何一种都可以
在连接 MySQL 后执行 set name XXX 指令(XXX 为你程序文件所使用字符集),通知 MySQL 以何种字符集与你会话



谢谢版主的指点;感觉自己太嫩了,没考虑那么多,我以为配到能用就行了,都没考虑到其他人;
 $conn = mysql_connect ( "localhost" , "root" , "" ) or    die ( " Could not connect: " . mysql_error ());    $conn . mysql_select_db ( "hong" );   mysql_set_charset( "utf8" , $conn );  //我原本是这样设置编码的, 现在改成mysql_query("set names utf8");就不会出现乱码       
Copy after login

mysql_client_encoding() 返回当前连接的默认字符集名称,
没设置编码前返回 latin1 ,用了mysql_query("set names utf8")还是返回latin1 ;这是为什么?

mysql_query("set names utf8")
只是通知 MySQL 用什么字符集会话,与客户端默认字符集无关
请注意 names 是 name 的复数形式,表示将所有相关的属性都改成 utf8,而不是仅客户端

mysql_query("set names utf8");$sql = "select * from table";mysql_query($sql) or die(mysql_error());
Copy after login

PHP中文乱码分类及解决办法大全,我以前也遇到过,参考这个解决了

http://www.lai18.com/content/312385.html

连接数据库的时候添加
mysql_query("set names utf8")
检查项目编码,数据库,表,字段的编码是否都统一

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles