<? 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.
================================================== ===========================
联系客服