MySQL 无法存储 UTF8 字符
找不到我无法在 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
Cannot find the reason why I am unable to store in a MySQL database characters like ţ, î, ş.
My table definition is:
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 ;
The connection to the database is done with the following script:
$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");
}
I have on the head part of the page:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
and the script is:
$text = 'ţ, î, ş';
mysql_query("insert into gen_admin_words_translated (word_id, lang_id, value, needUpd) values (1, 1, '$text', 1)");
All I get in the end in the table is:
SELECT * FROM `gen_admin_words_translated`
id word_id value lang_id needUpd
5166 1034 ?, 1 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当我运行你的脚本时,它对我有用:
结果:
要检查的事情:
检查你的网页是否真的是UTF-8,也许你在另一个地方设置了一些chaset。
文件编码也应该是UTF-8,否则可能会破坏你的字符..
as I ran your script it worked for me:
result:
things to check:
check if your webpage is really UTF-8, maybe you have some chaset set another place.
file encoding should be also UTF-8 as it may break your characters if otherwise ..
将我的评论扩展为答案:
看来您已经正确设置了所有内容,并且仅停留在将字符串文字插入数据库上。要成功完成此操作,您还必须确保保存的 PHP 脚本的文本编码也是 UTF-8。
大多数体面的编辑器都会让您知道您当前正在使用哪种编码,并且还可以另存为(即在不同编码之间进行转换)(即使现在的记事本也这样做)。但是,作为快速检查,您可以将字符
€
添加到文件中的某处并保存。如果文件大小改变了 1 或 2 个字节而不是 3 个字节,则说明您使用的不是 UTF-8,需要将文件转换为该编码。除此之外,当从浏览器接收文本作为输入时,您的代码应该可以很好地处理它。
注意:虽然使用
标记设置页面编码应该足够了,但最好使用来自 PHP 的 HTTP 标头来执行此操作,如下所示:
Expanding my comments into an answer:
It seems that you have set up things correctly, and are only stuck on inserting a string literal to the database. To do that successfully you must also ensure that your text encoding for the saved PHP script is also UTF-8.
Most decent editors will let you know which encoding you are currently working with and can also save as (i.e. convert between) different encodings (even Notepad does this today). However, as a quick check you can add the character
€
to your file somewhere and save it. If the file size changes by 1 or 2 bytes instead of 3, you are not on UTF-8 and you need to convert the file to that encoding.Other than that, when receiving text as input from the browser your code should handle it just fine.
Note: While using a
<meta>
tag to set the encoding for your page should be sufficient, it's better if you do this with an HTTP header from PHP like this:您粘贴的最后结果是否来自 MySQL 命令行?如果是,请在查询
SELECT * FROM gen_admin_words_translated
之前尝试SET NAMES utf8;
Does the last result you pasted come from MySQL Command-Line? If does, try
SET NAMES utf8;
before querySELECT * FROM gen_admin_words_translated
如果这
是您的文字代码,则需要确保PHP 源文件也编码为 UTF-8。否则,这些字符在 Unicode 上下文中将是 ISO-8859-1 字符,从而导致字符损坏。
If this:
is your literal code, you need to make sure that the PHP source file is encoded as UTF-8 as well. Otherwise, these characters will be ISO-8859-1 characters in a Unicode context, resulting in broken characters.
检查您的 MySQL 初始化文件。它应该包括这些字符集行:
Check your MySQL initialization file. It should include these character-set lines:
在此语句中,您将插入当前 PHP 文件中存在的字符:
但是,它们将使用 PHP 文件的字符编码进行编码。除非此 PHP 文件本身使用 UTF-8 编码,否则生成的字符串不会是 UTF-8 编码的。
您应该使用文本编辑器检查当前文件使用的字符编码。所有像样的文本编辑器都应该能够显示文档中使用的字符编码,有些可能能够转换。
要创建更可移植的代码,确保文档的字符编码无关紧要,您可以使用如下编码值:
不幸的是,如果您必须做很多这样的事情,那将会很痛苦,因为您必须使用多字节十六进制表示 - PHP 没有像其他语言那样指定字符的本机 Unicode 方式(您可以使用“\u163”而不是“\xC5\xA3”)。
您可以使用类似的工具查找十六进制的 UTF-8 表示形式。
In this statement, you are inserting characters as they exist in the current PHP file:
However, they will be encoded using the character encoding of your PHP file. Unless this PHP file uses UTF-8 encoding itself, the resulting string won't be UTF-8 encoded.
You should use your text editor to check the character encoding used on the current file. All decent text editors should be able to display, and some may be able to convert, the character encoding used in a document.
To create more portable code, ensuring the character encoding of your document doesn't matter, you can use encoded values like this:
Unfortunately, if you have to do a lot of this it'll be a pain, because you have to use the multi-byte hex representation - PHP doesn't have a native Unicode way of specifying characters like some other languages (where you can go "\u163" instead of "\xC5\xA3").
You can look up the UTF-8 representation in hex using tools like this.