开发者

MySQL query utf-8 characters (e.g. Chinese)(Also, I am using Doctrine)

开发者 https://www.devze.com 2023-04-03 17:54 出处:网络
$q = $this->createQuery(\'q\') ->where(\'q.group_id=?\', $group_id) ->andWhere(\'q.content=?\', $content)
$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 cau开发者_C百科ses 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号