MySQL 无法存储 UTF8 字符

发布于 2024-12-04 18:29:07 字数 1215 浏览 2 评论 0原文

找不到我无法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

挖个坑埋了你 2024-12-11 18:29:07

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

$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,否则可能会破坏你的字符..

as I ran your script it worked for me:

$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)

result:

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

things to check:

check if your webpage is really UTF-8, maybe you have some chaset set another place.

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

file encoding should be also UTF-8 as it may break your characters if otherwise ..

姐不稀罕 2024-12-11 18:29:07

将我的评论扩展为答案:

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

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

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

注意:虽然使用 标记设置页面编码应该足够了,但最好使用来自 PHP 的 HTTP 标头来执行此操作,如下所示:

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

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:

header('Content-type: text/html; charset=utf-8');
魔法少女 2024-12-11 18:29:07

您粘贴的最后结果是否来自 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 query SELECT * FROM gen_admin_words_translated

知足的幸福 2024-12-11 18:29:07

如果这

$text = 'ţ, î, ş';

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

If this:

$text = 'ţ, î, ş';

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.

感情废物 2024-12-11 18:29:07

检查您的 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

Check your MySQL initialization file. It should include these character-set lines:

[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
离线来电— 2024-12-11 18:29:07

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

$text = 'ţ, î, ş';

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

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

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

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

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

您可以使用类似的工具查找十六进制的 UTF-8 表示形式。

In this statement, you are inserting characters as they exist in the current PHP file:

$text = 'ţ, î, ş';

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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文