打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
mysql

<? Php
$ Conn = mysql_connect ("localhost", "root", "123456");
mysql_select_db ("db_bcty365", $ conn);
mysql_query ("set names gb2312");

Of which:

SET NAMES 'x' statement is equivalent to these three statements:

mysql> SET character_set_client = x;
mysql> SET character_set_results = x;
mysql> SET character_set_connection = x;

Is used to set the client character set, it should be said, has nothing to do with the server-side character set characters msyql server-side storage.

==============================================

MySQL character set
MySQL character set support (Character Set Support) has two aspects:
Character set (Character Set) and sorting (Collation).
Support for character sets refined to four levels:
Server (server), database (database) data sheet (table) and connection (connection).


Default character set 1.MySQL MySQL character set specified can be refined to a database, a table, one should be what characters

Sets.
However, the traditional program to create the database and data table does not use complex configuration, which is the default configuration, then

The default configuration come from?
(1) when compiling MySQL, specify a default character set, the character set is latin1;
(2) Install MySQL can be specified in the configuration file (my.ini) a default character set, if not specified, this value following the

Specified in the Order of the self-compiled;
(3) when starting mysqld, in the command-line argument to specify a default character set, if not specified, this value is inherited from the configuration

Configuration file, the this time character_set_server is set to the default character set;
(4) When you create a new database, unless explicitly specified, the database character set by default is set to

character_set_server;
(5) When a database is selected, the character_set_database is set to the database default character set;
(6) to create a table in the database, the table default character set is set to character_set_database is

Database default character set;
(7) when the set of a column in the table, unless explicitly specified otherwise in this column the default character set is the default character set;
Brief summary of what places are not modified, then all databases all tables in all the fields in with latin1 storage of

But if we install MySQL, usually choose multi-language support, that is, the installer automatically in the configuration file

default_character_set set to UTF-8 This ensures that the default, all fields of the database table

UTF-8 storage.

View the default character set (by default, MySQL character set is latin1 (ISO_8859_1)
Typically, view the system's character set and sort by setting the following two commands:
mysql> SHOW VARIABLES LIKE 'character%';
+ -------------------------- + ---------------------- ----------- +
| Variable_name | Value |
+ -------------------------- + ---------------------- ----------- +
| Character_set_client | latin1 |
| Character_set_connection | latin1 |
| Character_set_database | latin1 |
| Character_set_filesystem | binary |
The | character_set_results | latin1 |
|, Character_set_server | latin1 |
| Character_set_system | utf8 |
| Character_sets_dir | D: "mysql-5.0.37" share "charsets" |
+ -------------------------- + ---------------------- ----------- +

mysql> SHOW VARIABLES LIKE 'collation_%';
+ ---------------------- + ----------------- +
| Variable_name | Value |
+ ---------------------- + ----------------- +
| Collation_connection | utf8_general_ci |
| Collation_database | utf8_general_ci |
| Collation_server | utf8_general_ci |
+ ---------------------- + ----------------- +

Modify the default character set
(1) The simplest modification is a change in the character set in mysql my.ini file key,
Such as default-character-set = utf8
character_set_server = utf8
After modification, restart the mysql service, service mysql restart
Use mysql> SHOW VARIABLES LIKE 'character%'; look and found the database encoding have been changed to utf8
+ -------------------------- + ---------------------- ----------- +
| Variable_name | Value |
+ -------------------------- + ---------------------- ----------- +
| Character_set_client | utf8 |
| Character_set_connection | utf8 |
| Character_set_database | utf8 |
| Character_set_filesystem | binary |
The | character_set_results | utf8 |
|, Character_set_server | utf8 |
| Character_set_system | utf8 |
| Character_sets_dir | D: "mysql-5.0.37" share "charsets" |
+ -------------------------- + ---------------------- ----------- +

(2) there is a method to change the character set is to use the mysql command
mysql> SET character_set_client = utf8;
mysql> SET character_set_connection = utf8;
mysql> SET character_set_database = utf8;
mysql> SET character_set_results = utf8;
mysql> SET character_set_server = utf8;

mysql> SET collation_connection = utf8;
mysql> SET collation_database = utf8;
mysql> SET collation_server = utf8;


Generally even set the table default character set to utf8 and query sent by the UTF-8 encoding, you will find still stored in the database is garbled

. The problem lies in this connection the connection layer. The solution is to send a query to perform the following phrase: SET NAMES

'Utf8';
It is equivalent to the following three commands:
SET character_set_client to = utf8;
SET The character_set_results to = utf8;
SET character_set_connection to = utf8;

Summary:
So, what version of the database, no matter 3.x, or 4.0.x or 4.1.x, in fact, is not important for us, it is important for two:
1) set the correct database encoding. Below MySQL4.0 version of the character set is always the default ISO8859-1, MySQL 4.1 installed

Let you choose. If you are ready to use UTF-8, is necessary to create the database specified in UTF-8 (created can be changed,

The 4.1 version also can be used alone to specify the character set of the table)
2) set the correct database connection encoding to set the encoding of the database, connect to the database, should be specified.

================================================== ===========================

The mysql5 provide the following character set system variables:
the character_set_client client character set
the character_set_connection client and server-side connection character set used
the The character_set_results SELECT query return the character set
the database using character_set_database character set garbled generally set according to the error due to the above variables, so as long as you understand these variables, you can say goodbye to distortion.
Using the above variables, to understand the core idea: character_set_client, character_set_connection these two variables is guaranteed to be consistent with character_set_database encoded character_set_results guarantee the SELECT returns results with program coding.
We can set names used in the program to set the character_set_client, character_set_connection, character_set_results three system variables.

For example, the set names 'utf8' is equivalent to:

set @ @ character_set_client = 'utf8'

set @ @ character_set_connection = 'utf8'
set @ @ character_set_results = 'utf8'
Under normal circumstances, when the character set of the database and the database table to utf8 in the program settings set Names 'utf8' command, so that we can ensure that no distortion, but also pay attention to here value of the character_set_results variable, The character_set_results characters The value is used to display the encoded returned to the user.

For example, you the database (character_set_database) with the utf8 character set, then you necessary to guarantee character_set_client, character_set_connection, is also utf8 character set. Your program may not UTF8, such as your program is gbk, so if you character_set_results set to utf8, then there will be garbled. At this point you should character_set_results set for gbk. This will ensure that the database returns the results of your program coding.
The following excerpt from a block of the network:

<?
/ / If the program uses the utf8 character set
$ Program_char = 'utf8';
/ / Mysql version number of the first check, if the version number is greater than 4 we can set these system variables (the these systems Mysql4 variable)
$ Version = current ($ db-> fetch_one ('SELECT VERSION ()'));
if (substr ($ version, 0, 1)> 4)
{
/ / Remove the current database character set
$ Sql ??= 'SELECT @ @ character_set_database';
$ Char = current ($ db-> fetch_one ($ sql));
/ / Client character set (character_set_client) and connection character set (character_set_connection) is set to the database character set (character_set_database)
$ Db-> query ('SET @ @ character_set_client = "'. $ Char. '"');
$ Db-> query ('SET @ @ character_set_connection = "'. $ Char. '"');
/ / SELECT query to return data character set to the character set consistent with the current program
$ Db-> query ('SET @ @ character_set_results = "'. $ Program_char. '"');
}



1, to ensure that the data stored in the database and the database encoding, data encoding and character_set_database consistent;
2, to ensure that communication character set and the database character set, that is, character_set_client, character_set_connection character_set_database consistent;
3, to ensure that the the the SELECT return program coding, character_set_results program code;
4, to ensure that the program coding and browser encoding, program coding and <meta http-equiv="Content-Type" content="text/html; charset=?"/>.
================================

----------------------------------
the mysql versions of 5
mysql database encoding is set to UTF-8
PHP page <meta http-equiv= "Content-Type "content= "text/html; charset=utf-8" />
PHP code
mysql_query ("set names 'UTF-8'"); also set up a unified
However, Chinese, Korean displayed are garbled, why ah you! ! ! I am so unified coding can be garbled, crazy
Solving! ! !

Analysis:
(1) mysql_query ("set names 'UTF8'");
(2) recognize that mysql_query ("set names 'utf8'"); mysql_query ("set names 'utf-8'"); difference should not have any problems A:
PHP codemysql_query ("set names utf8");
And not UTF-8.
Summary: mysql_query ("set name 'utf8'") this is right.
(3) page appears garbled, and from four to analyze:
Database used in coding;
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>;
The php specified under the Code (set name 'utf8');
Page encoding (the page with Notepad to open the Save As, look at the encoding used by the page)
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
MySQL5中文乱码解决 - 前前 - JavaEye技术网站
关于MySQL如何修改character
mysql的utf8设置
mysql命令行修改字符编码
mysql 重装出现乱码设置为utf8解决的介绍
MySQL : create database, drop database
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服