找不到我無法存儲在MySQL數據庫字符如ţ,,,ş的原因。不能在MySQL中存儲UTF8字符
我的表的定義是:
CREATE TABLE IF NOT EXISTS `gen_admin_words_translated` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`word_id` int(10) NOT NULL,
`value` text COLLATE utf8_unicode_ci,
`lang_id` int(2) NOT NULL,
`needUpd` int(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2689 ;
到數據庫的連接是用下面的腳本來完成:
$charset = "UTF8";
$link = mysql_connect($host, $user, $pass);
if(!$link){
die("Unable to connect to database server.");
}
mysql_selectdb($database);
if(function_exists("mysql_set_charset")){
mysql_set_charset($charset, $link);
}else{
mysql_query("SET NAMES $charset");
}
我對頁面的頭部:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
和腳本是:
$text = 'ţ, î, ş';
mysql_query("insert into gen_admin_words_translated (word_id, lang_id, value, needUpd) values (1, 1, '$text', 1)");
所有我在表到底得的是:
SELECT * FROM `gen_admin_words_translated`
id word_id value lang_id needUpd
5166 1034 ?, 1 1
由於您試圖插入字符文字...您的腳本的文本編碼(保存在磁盤上)也是UTF-8? – Jon
@Jon:我該如何檢查?我在服務器上運行腳本,而不是在本地運行。 – CristiC
取決於您的編輯器,但您也可以執行快速和骯髒的檢查:將字符'€'添加到您的文件並保存。如果文件大小更改爲1或2個字節而不是3個,則表示不在UTF-8上。 – Jon