MySQL查询utf-8字符(例如中文)(另外,我使用的是Doctrine)

发布于 2024-12-04 01:17:13 字数 402 浏览 6 评论 0 原文

$q = $this->createQuery('q')
->where('q.group_id=?', $group_id)
->andWhere('q.content=?', $content)
    ->execute();

如果我的 $content 包含任何 unicode 字符(例如中文/日文),则会导致以下消息:

SQLSTATE[HY000]: General error: 1267 Illegal mix of collations 
(latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='

以前有人遇到过类似的问题吗?

$q = $this->createQuery('q')
->where('q.group_id=?', $group_id)
->andWhere('q.content=?', $content)
    ->execute();

If my $content contains any unicode characters (e.g. Chinese/japanese) this causes the following message:

SQLSTATE[HY000]: General error: 1267 Illegal mix of collations 
(latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='

Any one encountered similar problem before?

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

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

发布评论

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

评论(1

じее 2024-12-11 01:17:13

您可以在 where 子句中使用 MySQL 的 COLLATE 函数,需要将入站数据转换为列排序规则 (latin1_swedish_ci)

$q = $this->createQuery('q')
->where('q.group_id=?', $group_id)
->andWhere('q.content = _latin1 ? COLLATE latin1_swedish_ci', $content)
    ->execute();

有关 collat​​e 函数的详细信息,您可以查看 http://dev.mysql.com/doc/refman/5.6/en/charset-collat​​e.html 其中有详细信息,这从 4.1 开始就存在于 mysql 中。

您还可以在表结构的定义中设置每列的排序规则(请参阅http://dev.mysql.com/doc/refman/5.6/en/charset-column.html)了解详细信息。

希望这有帮助。

You can use the COLLATE function with MySQL in the where clause, will need to convert the inbound data to the column collation (latin1_swedish_ci)

$q = $this->createQuery('q')
->where('q.group_id=?', $group_id)
->andWhere('q.content = _latin1 ? COLLATE latin1_swedish_ci', $content)
    ->execute();

For details about the collate function you can have a look at http://dev.mysql.com/doc/refman/5.6/en/charset-collate.html which has the details, this have been in mysql from 4.1.

You can also set the collation per column as well in the definition of the table structure (see http://dev.mysql.com/doc/refman/5.6/en/charset-column.html) for details.

Hope this helps.

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