:: DEVELOPER ZONE
MySQL allows names that consist of a single identifier or
multiple identifiers. The components of a multiple-part name
should be separated by period
(‘.’) characters. The initial
parts of a multiple-part name act as qualifiers that affect the
context within which the final identifier is interpreted.
In MySQL you can refer to a column using any of the following forms:
| Column Reference | Meaning |
col_name |
The column col_name from whichever table used
in the statement contains a column of that name. |
tbl_name.col_name |
The column col_name from table
tbl_name of the default
database. |
db_name.tbl_name.col_name |
The column col_name from table
tbl_name of the database
db_name. |
If any components of a multiple-part name require quoting, quote
them individually rather than quoting the name as a whole. For
example, write `my-table`.`my-column`, not
`my-table.my-column`.
如果 任何 多部分 名字(multiple-part name) 要求 用引号quoting 是 `my-table`.`my-column`不是 `my-table.my-column`
` This character is under Esc ,above Tab 叁考下面:除了VALUES ('烤地瓜'.......)不是 , 其他全部都是,但是 不是规定必须要用 这点 `........moreCREATE DATABASE `daily-food` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; USE `daily-food` ; CREATE TABLE `healthy-food` ( `早上` VARCHAR( 50 ) NULL , `中午` VARCHAR( 50 ) NOT NULL , `晚上` VARCHAR( 50 ) NULL ) TYPE = innodb; INSERT INTO `healthy-food` ( `早上` , `中午` , `晚上` ) VALUES ( '烤地瓜', '林光常にての健康餐', '全麦面包' );
下面的例子 可看出 必须要用 这点 ` 的情况
mysql> drop database daily-food ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '-food
' at line 1
mysql> drop database `daily-food` ;
Query OK, 1 row affected (0.06 sec)
You need not specify a tbl_name or
db_name.tbl_name prefix for a column
reference in a statement unless the reference would be
ambiguous. Suppose that tables t1 and
t2 each contain a column
c, and you retrieve c in a
SELECT statement that uses both
t1 and t2. In this case,
c is ambiguous because it is not unique among
the tables used in the statement. You must qualify it with a
table name as t1.c or t2.c
to indicate which table you mean. Similarly, to retrieve from a
table t in database db1
and from a table t in database
db2 in the same statement, you must refer to
columns in those tables as
db1.t. and
col_namedb2.t..
col_name
A word that follows a period in a qualified name must be an identifier, so it is not necessary to quote it, even if it is a reserved word.
The syntax .tbl_name means the table
tbl_name in the default database.
This syntax is accepted for ODBC compatibility because some ODBC
programs prefix table names with a
‘.’ character.
mysql> SET NAMES 'utf8' ; Query OK, 0 rows affected (0.00 sec) mysql> SHOW VARIABLES LIKE '%collation%' ; +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-----------------+ 3 rows in set (0.00 sec) mysql> SHOW VARIABLES LIKE '%character%' ; +--------------------------+-------------------------------+ | Variable_name | Value | +--------------------------+-------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | c:\wamp\mysql\share\charsets\ | +--------------------------+-------------------------------+ 7 rows in set (0.00 sec) mysql> SELECT * FROM `healthy-food` ; +-----------+-----------------------------+------------+ | ?拐? | 銝剖? | ?? | +-----------+-----------------------------+------------+ | ?文??| ??撣詻?艾?亙熒擗?| ???W? | +-----------+-----------------------------+------------+ 1 row in set (0.00 sec)
我想妳不需要改變mysql的編碼(default-character-set=latin1修改如
default-character-set=big5
),因為即使妳讓mysql以latin編碼保存信息,從數據庫提取信息時,隻要page本身知道它的編碼就好叻。
比如php程式有一段big5文字要保存。我們用mysql query將一段big5文字傳到msql服務器,
這是假設mysql設置為默認latin1,mysql服務器將以latin1的方式把文字保存到數據庫中。
php程式query數據時,mysql服務器以latin1的方式讀囬數據,傳遞到php程式。此時,php程式將其辨認為big5(根據頁麵的內碼設置),因此妳又得到叻原來的big5文字。
其實主要是php程式裏要設定適當的頁內碼,跟mysql沒有太大關繫。
當然還是有一點不方便。
此時妳用phpmyadmin 登陸查看數據庫中的信息,默認內碼為latin1,妳看到的數據庫中的big5文字是亂碼。
不過隻要妳手動在ie中選擇頁內碼big5,卻仍然可以看到正常的big5文字。
简单的总结一下,如果什么地方都不修改,
那么所有的数据库的所有表的所有栏位的都用 latin1 存储,
不过我们如果安装 MySQL,
一般都会选择多语言支持,也就是说,
安装程序会自动在配置文件中把 default_character_set 设置为 UTF-8,
这保证了缺省情况下,所有的数据库的所有表的所有栏位的都用 UTF-8 存储。
当一个 PHP 程序与 MySQL 建立连接后,
这个程序发送给 MySQL 的数据采用的是什么字符集?
MySQL 无从得知 (它最多只能猜测),所以 MySQL 4.1 要求客户端必须指定这个字符集,
也就是 character_set_client,
MySQL 的怪异之处在于,得到的这个字符集并不立即转换为存储在
数据库中的那个字符集,
而是先转换为 character_set_connection 变量指定的一个字符集;
这个 connection 层究竟有什么用我不大明白,
但转换为 character_set_connection 的这个字符集之后,
还要转换为数据库默认的字符集,也就是说要经过两次转换;
当这个数据被输出时,
又要由数据库默认的字符集转换为 character_set_results 指定的字符集。
一个典型的环境
典型的环境以我自己的电脑上安装的 MySQL 4.1 为例,
我自己的电脑上安装着 Apache 2,PHP 5 和 WordPress 1.5.1.3,
MySQL 配置文件中指定了 default_character_set 为 utf8。
于是问题出现了:
WordPress 按照默认情况安装,所以所有的表都用 UTF-8 存储数据;
WordPress 默认采用的浏览字符集是 UTF-8 (Options->Reading 中设置),
因此所有 WP 页面的 meta 中会说明 charset 是 utf-8;
所以浏览器会以 utf-8 方式显示所有的 WP 页面;
这样一来 Write 的所有 Post,和 Comment 都会以
UTF-8 格式从浏览器发送给 Apache,再由 Apache 交给 PHP;
所以 WP 从所有的表单中得到的数据都是 utf-8 编码的;
WP 不加转换的直接把这些数据发送给 MySQL;
MySQL 默认设置的 character_set_client 和
character_set_connection 都是 latin1,
此时怪异的事情发生了,实际上是 utf-8 格式的数据,
被“当作 latin1”转换成……居然还是转换成 latin1,
然后再由这个 latin1 转换成 utf-8,这么两次转换,
有一部分 utf-8 的字符就丢失了,变成 ??,
最后输出的时候 character_set_results 默认是 latin1,
也就输出为奇怪的东西了。最神奇的还不是这个,
如果 WordPress 中设置以 GB2312 格式阅读,
那么 WP 发送给 MySQL 的 GB2312 编码的数据,
被“当作 latin1”转换后,存进数据库的是一种奇怪的格式
(真的是奇怪的格式,mysqldump 出来就能发现,
无论当作 utf-8 还是当作 gb2312 来读都是乱码),
但如果这种格式以 latin1 输出出来,居然又能变回 GB2312!
这会导致什么现象呢?WP 如果使用 MySQL 4.1 数据库,
把编码改用 GB2312 就正常了,可惜,这种正常只是貌似正常。
mysql> SHOW VARIABLES LIKE '%character%' ;
+--------------------------+-------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | c:\wamp\mysql\share\charsets\ |
+--------------------------+-------------------------------+
7 rows in set (0.01 sec)
mysql> SET CHARACTER_SET_CLIENT='gbk' ;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE '%character%' ;
+--------------------------+-------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------+
| character_set_client | gbk |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | c:\wamp\mysql\share\charsets\ |
+--------------------------+-------------------------------+
7 rows in set (0.00 sec)
mysql> SET CHARACTER_SET_RESULTS='gbk' ;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE '%character%' ;
+--------------------------+-------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------+
| character_set_client | gbk |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_results | gbk |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | c:\wamp\mysql\share\charsets\ |
+--------------------------+-------------------------------+
7 rows in set (0.00 sec)
mysql> CREATE DATABASE raisin DEFAULT CHARACTER SET gbk
-> DEFAULT COLLATE gbk_chinese_ci;
Query OK, 1 row affected (0.02 sec)
mysql> use raisin ;
Database changed
mysql> select * from record ;
+----------+--------+------+------+------+
| IDnumber | 俷靡 | 弊恅 | 荎恅 | math |
+----------+--------+------+------+------+
| C890686 | 笚? | 41 | 14 | 33 |
| D890604 | ?悕衭 | 99 | 97 | 87 |
| E890605 | 輿恅鍍 | 99 | 77 | 57 |
| F890606 | 輿恅珨 | 28 | 11 | 33 |
| G890607 | 輿湮祩 | 98 | 100 | 100 |
| H890609 | 輿湮? | 99 | 74 | 89 |
| I890610 | 笚湮? | 41 | 47 | 49 |
| J890613 | 輿腦華 | 92 | 92 | 88 |
| K890614 | 輿湮? | 39 | 19 | 29 |
| L890615 | 笚苤隴 | 98 | 90 | 91 |
| M890616 | 笚珨 | 47 | 50 | 55 |
| N890617 | 笚苤鏗 | 94 | 94 | 100 |
| V890618 | 輿湮珨 | 79 | 85 | 77 |
| U890619 | 燠隴睿 | 84 | 74 | 58 |
| T890620 | 笚尪豌 | 94 | 57 | 64 |
| S890621 | 輿恅? | 83 | 80 | 94 |
| R890622 | 枎蚗 | 71 | 75 | 73 |
| O890623 | 輿湮 | 20 | 25 | 10 |
| P890624 | 輿昝堊 | 93 | 100 | 90 |
| Q890625 | 笚湮 | 68 | 57 | 39 |
+----------+--------+------+------+------+
20 rows in set (0.00 sec)
Mind the following character_set_database | gbk has changed
could be changed by CREATE DATABASE raisin DEFAULT CHARACTER SET gbk ?
mysql> SHOW VARIABLES LIKE '%character%' ;
+--------------------------+-------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------+
| character_set_client | gbk |
| character_set_connection | latin1 |
| character_set_database | gbk |
| character_set_results | gbk |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | c:\wamp\mysql\share\charsets\ |
+--------------------------+-------------------------------+
为什么在phpmyadmin中以插入记录的方式输入的中文能在phpmyadmin中正常浏览,而输出到网页却是?号;
从网页提交到数据库中的中文在phpmyadmin中显示为乱码,输出到网页却是正常的?
服务器是用IAMP建的,他是别人作好的PHP+MYSQL+Apache,
版本分别是:MySQL 4.1.9 Apache2.0.52 PHP 5.0.3
网上说这种问题的文章很多,但都不太适用于我,一些配置文件中相关的项目好象也是支持中文的,象latin1在我的MY.INI中就没有,真不知道该怎么办呀,请广大高手指教呀!