MySQL 排序规则

发布于 2024-12-03 20:15:20 字数 221 浏览 1 评论 0原文

我在 PHP 和 MySQL 上开发时遇到了一个非常奇怪的问题。 我四五年前就遇到过这样的问题,但从那以后就没有了。而且真的不记得我是如何解决的。

好吧...问题:我的数据库具有 utf_unicode_ci 排序规则,但是在插入格鲁吉亚字母时: სახელი და გვარი 在数据库中我有 áfâáς~áς®áς”á▪▪▪<- 这个符号。问题可能是什么?以及如何解决?

有什么想法吗?

I faced a very strange problem when developing on PHP and MySQL.
I had such problem 4-5 years ago, but since than not. And really don't remember how I solved it.

Well... the problem: I'm having database with collation of utf_unicode_ci, but when inserting georgian letters: სახელი და გვარი in the database I'm having მიხეირ<- this symbols. What could the problem be? and how to solve it?

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

π浅易 2024-12-10 20:15:21

我可以向您保证的一件事是,您遇到的问题与整理无关。它与字符集和编码有关,很可能问题不是在将数据插入数据库时​​,而是在读取数据并显示它们时,mysql中有很多关于unicode字符的问题,所以进行搜索,你会发现大量的提示和信息。

One thing that I can make you sure of is that the problem you are experiencing doesn't have anything to do with collation. It is related to character set and encoding and most probably the problem is not when inserting the data into database but when you read the data and display them, there are many questions about unicode characters in mysql on SO do a search and you'll find tons of tips and information.

甜扑 2024-12-10 20:15:20

根据Eray的回答,我意识到 utf8_general_ci 应该在排序规则中设置,而不是 utf8_unicode_ci

并且除此之外使用 mysql_set_charset('utf8', $connection) 函数。

这是示例:

        $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
    mysql_set_charset('utf8',$connection);      
    if (!$connection) {
        die("Database connection failed: " . mysql_error());
    } else {
        $db_select = mysql_select_db(DB_NAME, $connection);
        if (!$db_select) {
            die("Database selection failed: " . mysql_error());
        }
    }
}

所以在第二行使用 mysql_set_charset 解决了我的问题。

希望它对任何人都有帮助..

According to Eray's answer I realized that utf8_general_ci shuld be set in collation and not utf8_unicode_ci

and in addition to that use mysql_set_charset('utf8', $connection) function.

Here's the sample:

        $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
    mysql_set_charset('utf8',$connection);      
    if (!$connection) {
        die("Database connection failed: " . mysql_error());
    } else {
        $db_select = mysql_select_db(DB_NAME, $connection);
        if (!$db_select) {
            die("Database selection failed: " . mysql_error());
        }
    }
}

So having mysql_set_charset on the second line solved my issue.

Hope it helps anyone..

喜你已久 2024-12-10 20:15:20

您可以尝试 utf8_general_ci 而不是这个吗?
它会解决你的问题。

但不要忘记,*databases* 和 *tables* 排序规则必须是 utf8_general_ci

Can you try utf8_general_ci instead of this ?
It will solve your problem.

But don't forget , *databases* and *tables* collation must be utf8_general_ci.

八巷 2024-12-10 20:15:20

确保您使用的是带有 N 前缀的字符串类型之一(NCHAR、NVARCHAR)。

添加数据时,不要忘记在传递给数据库的参数中添加N。例如:

INSERT INTO table VALUES (N'Georgian Letters', N'Georgian Letters', ...)

N 前缀将告诉 sql 其后续字符串是 unicode 字符串。

Make sure you're using one of the string types with N prefix (NCHAR, NVARCHAR).

Don't forget to add N to the parameters you are passing to the database when adding data. For example:

INSERT INTO table VALUES (N'Georgian Letters', N'Georgian Letters', ...)

The N prefix will tell sql its subsequent string is a unicode string.

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