tags:

views:

127

answers:

2

I'm getting this strange error while processing a large number of data...

Error Number: 1267

Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='

SELECT COUNT(*) as num from keywords WHERE campaignId='12' AND LCASE(keyword)='hello again 昔 から ある å ´æ‰€'

What can I do to resolve this? Can I escape the string somehow so this error wouldn't occur, or do I need to change my table encoding somehow, and if so, what should I change it to?

+2  A: 

SET collation_connection = 'utf8_general_ci'

then for your databases

ALTER DATABASE db CHARACTER SET utf8 COLLATE utf8_general_ci

ALTER TABLE table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

MySQL sneaks swedish in there sometimes for no sensible reason.

Ben Hughes
+1  A: 

You should set both your table encoding and connection encoding to UTF-8:

ALTER TABLE keywords CHARACTER SET UTF8; -- run once

and

SET NAMES 'UTF8';
SET CHARACTER SET 'UTF8';
Quassnoi
Are both of these needed, or can i just do one of them?
Click Upvote
ALTER DATABASE `myDb` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin . Would that work? This is done so it would affect all of my tables, not just one of them
Click Upvote
ALTER DATABASE won't change your current table settings, only newly created ones. It though won't hurt to alter default charset for database too.
Quassnoi
SET NAMES and SET CHARACTER SET will change your connection encoding. You need to issue these commands each time your connect. You client library may support more elegant method to do it (php::mysqli does, php::mysql does not).
Quassnoi
Seems to work for now, i'll accept after some more testing. Do the second queries need to be run once, or at the start of each script?
Click Upvote
Once per connection.
Quassnoi
Still not working, man. Error Number: 1267Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='SELECT COUNT(*) as num from keywords WHERE campaignId='12' AND LCASE(keyword)='hello again 昔 から ある å ´æ‰€'
Click Upvote
Could you please run the following: SHOW CREATE TABLE keyword; and post the result here?
Quassnoi
The other person's solution worked so i;ve accepted
Click Upvote