无法在MySQL中存储UTF8字符

时间:2011-09-16 07:24:26

标签: php mysql utf-8 character-encoding

无法找到我无法在MySQL数据库中存储ţ,î,ş等字符的原因。

我的表定义是:

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

6 个答案:

答案 0 :(得分:4)

当我运行你的脚本时,它对我有用:

$charset = "UTF8";
$link = mysql_connect('localhost', 'root', '') or die('connection?');
mysql_select_db('test') or die('database?');
if(function_exists("mysql_set_charset")){
    mysql_set_charset($charset, $link);
}else{
    mysql_query("SET NAMES $charset");   
}

$text = 'ţ, î, ş';
mysql_query("insert into gen_admin_words_translated (word_id, lang_id, value, needUpd) values (1, 1, '$text', 1)");

$query = mysql_query('SELECT * FROM  `gen_admin_words_translated`');
$array = mysql_fetch_array($query);

print_r($array)

结果:

Array
(
    [0] => 2689
    [id] => 2689
    [1] => 1
    [word_id] => 1
    [2] => ţ, î, ş
    [value] => ţ, î, ş
    [3] => 1
    [lang_id] => 1
    [4] => 1
    [needUpd] => 1
)

要检查的事项:

检查你的网页是否真的是UTF-8,也许你有一些chaset设置了另一个地方。

header('Content-type: text/html; charset=utf-8');

文件编码也应该是UTF-8,否则可能会破坏你的字符..

答案 1 :(得分:3)

将我的评论扩展为答案:

您似乎已正确设置了内容,并且只是在将字符串文字插入数据库时​​停留。要成功完成此操作,您还必须确保已保存的PHP脚本的文本编码也是UTF-8

大多数体面的编辑都会让你知道你正在使用哪种编码,也可以保存为(即转换)不同的编码(即使记事本今天这样做)。但是,作为快速检查,您可以将字符添加到您的文件中并保存。如果文件大小改变1或2个字节而不是3,则不是UTF-8,而是需要将文件转换为该编码。

除此之外,当从浏览器接收文本作为输入时,您的代码应该处理它。

注意:虽然使用<meta>标记来设置页面的编码应该已经足够了,但如果您使用PHP的HTTP标头执行此操作会更好:

header('Content-type: text/html; charset=utf-8');

答案 2 :(得分:2)

您粘贴的最后一个结果是来自MySQL命令行吗?如果是,请在查询SET NAMES utf8;

之前尝试SELECT * FROM gen_admin_words_translated

答案 3 :(得分:2)

如果:

$text = 'ţ, î, ş';

是您的文字代码,您需要确保 PHP源文件也编码为UTF-8 。否则,这些字符在Unicode上下文中将是ISO-8859-1字符,从而导致字符损坏。

答案 4 :(得分:2)

检查MySQL初始化文件。它应该包括这些字符集行:

[client]
port=3306

[mysql]
default-character-set=utf8
port = 3306
#
[mysqld]
basedir=".....
#Path to the database root
datadir=".....
# The default character set that will be used when a new schema or table is
# created and no character set is defined
character-set-server=utf8

答案 5 :(得分:1)

在此语句中,您将插入当前PHP文件中存在的字符:

$text = 'ţ, î, ş';

但是,它们将使用PHP文件的字符编码进行编码。除非此PHP文件本身使用UTF-8编码,否则生成的字符串将不是UTF-8编码。

您应该使用文本编辑器检查当前文件中使用的字符编码。所有体面的文本编辑器都应该能够显示文档中使用的字符编码,并且有些人可能能够转换。

要创建更多可移植代码,确保文档的字符编码无关紧要,您可以使用如下编码值:

$text = "\xC5\xA3, \xC3\xAE, \xC5\x9F";

不幸的是,如果你必须做很多这样的事情,那将是一件痛苦的事情,因为你必须使用多字节十六进制表示 - PHP没有像其他语言那样指定字符的原生Unicode方式(你可以去“\ u163”而不是“\ xC5 \ xA3”。)

您可以使用tools like this以十六进制查找UTF-8表示。