最近关于中文显示乱码的贴子比较多,所以也做了个总结: 可以参考一下杨涛涛版主的《各种乱码问题汇总》 http://topic.csdn.net/u/20071124/08/3b7eae69-ed1d-4a77-8895-9930bf3601af.html MySQL字符集的原理介绍。摘录于官方文档。http://dev.mysql.com/doc
最近关于中文显示乱码的贴子比较多,所以也做了个总结:
可以参考一下杨涛涛版主的《各种乱码问题汇总》
http://topic.csdn.net/u/20071124/08/3b7eae69-ed1d-4a77-8895-9930bf3601af.html
MySQL字符集的原理介绍。摘录于官方文档。http://dev.mysql.com/doc/refman/5.1/zh/charset.html
不同的编码格式会导致同一字符,在不同字符集下的编码会不同。同样同一编码在不同的字符集中代码的字符也不相同。当你的MySQL返回的字符串的编码格式(字符集)与你的客户工具程序(mysql, php, query browser, ...)当前使用的字符集不同时,就会造成乱码。 比如一个英国朋友告诉你Long, 当一位中国小学生看到后就会告诉你“龙”而不是“长”
关于字符集的详细介绍和例子,建议花一点时间看一下
http://dev.mysql.com/doc/refman/5.1/zh/charset.html (第10章:字符集支持)。
这里仅摘要一下。
MySQL中默认字符集的设置有四级:服务器级,数据库级,表级 。最终是字段级 的字符集设置。注意前三种均为默认设置,并不代码你的字段最终会使用这个字符集设置。所以我们建议要用show create table table ; 或show full fields from tableName; 来检查当前表中字段的字符集设置。
MySQL中关于连接环境的字符集设置有 Client端,connection, results 通过这些参数,MySQL就知道你的客户端工具用的是什么字符集,结果集应该是什么字符集。这样MySQL就会做必要的翻译,一旦这些参数有误,自然会导致字符串在转输过程中的转换错误。基本上99%的乱码由些造成。
乱码后需要检查的信息。(如果需要论坛上的朋友帮助,建议你提供以下信息 )
1. 数据库表中字段的字符集设置 。show create table TableName
或 show full columns from tableName
mysql>
show create table t1;
+-------+------------------------------------
| Table | Create Table
+-------+------------------------------------
| t1 | CREATE TABLE `t1` (
`id` int(11) NOT NULL,
`c1` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+------------------------------------
1 row in set (0.00 sec)
mysql> show full columns from t1;
+-------+-------------+----------------+------+-----+-
| Field | Type | Collation | Null | Key |
+-------+-------------+----------------+------+-----+-
| id | int(11) | NULL | NO | PRI |
| c1 | varchar(30) | gbk_chinese_ci | YES | |
+-------+-------------+----------------+------+-----+-
2 rows in set (0.00 sec)
mysql>
2. 当前联接系统参数 show variables like 'char%'
mysql>
show variables like 'char%';
+--------------------------+----------------
| Variable_name | Value
+--------------------------+----------------
| character_set_client | gbk
| character_set_connection | gbk
| character_set_database | latin1
| character_set_filesystem | binary
| character_set_results | gbk
| character_set_server | latin1
| character_set_system | utf8
| character_sets_dir | C:/Program File
+--------------------------+----------------
8 rows in set (0.00 sec)
mysql>
1. 中文,请确保 表中该字段的字符集为中文兼容:
big5 | Big5 Traditional Chinese
gb2312 | GB2312 Simplified Chinese
gbk | GBK Simplified Chinese
utf8 | UTF-8 Unicode
2. 确保,联接参数与这个字段字符集一致,你可以用 set name 'charsetname';
比如, set name 'gbk';
这条命令会同时修改 character_set_client,character_set_connection,character_set_results
(如果你的这架MySQL中都为中文,则你可以在my.ini或my.cnf中加上或修改这个参数, 参数文件修改后需重启MySQL服务)
[mysql]
default-character-set=gbk
3. PHP 乱码, 同样 mysql_query("set name 'gbk'"); 其它API也类似。
4. phpmyadmin里乱码
phpMyAdmin的config.inc.php中有没有设置$cfg['DefaultCharset']='utf-8';
5. Windows操作系统中命令行("DOS"窗口)下。
在你的DOS窗中的左上角标题栏片左键,属性,
在字体中,选择“宋体”,确认
mysql中 set names 'gbk';
6. ADO.NET, ADO中 ,可以连接字符串中加入CharSet=UTF8;类似指令以说明connection的字符集。
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; CharSet=UTF8;
7. SQL Manager for MySQL
用EMS建数据库,
Character Set设为utf-8
client charset设UTF-8
Font charset 设为GB2312_CHARSET
8. jdbcodbc桥接
http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/bridge.html
//
Load the JDBC-ODBC bridge driver
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver) ;
// setup the properties
java.util.Properties prop =
new
java.util.Properties();
prop.put( "
charSet " ,
" Big5
" );
prop.put( "
user " , username);
prop.put( "
password "
, password);
// Connect to the database
con =
DriverManager.getConnection(url, prop);
9. PHP 5.2 版本以上解决乱码问题的一个方法 (由 ljf_ljf [Mark Liang] 提供)
$conn =
mysql_connect
( "
192.168.1.133 "
, "
root " ,
"
123456 " ) or
die
( " Could not connect:
" .
mysql_error
());
$program_char = "
utf8 "
;
$conn .
mysql_select_db (
" test
" );
// $conn.mysql_query('SET @@character_set_results = "'.$program_char.'"');
mysql_set_charset( $program_char
, $conn );
$charset = mysql_client_encoding
( $conn
);
printf
( " current character set is %s
" , $charset );
$result = mysql_query
( "
SELECT id, task_no,pack_path FROM tb_workplan where id = 1 "
, $conn
);
while (
$row =
mysql_fetch_array
( $result
, MYSQL_BOTH)) {
printf
( " ID: %s
task_no: %s
pack_path :%s
" , $row [
" id
" ] ,
$row
[ 1 ]
, $row
[ "
pack_path "
]);
}
$conn .
mysql_free_result
( $result );
$conn .
mysql_close ();
9. 存储过程参数乱码
create procedure t ( aa char(10) charset 'gbk')
未完。。。

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools
