Search code examples
phpencodingutf-8latin1

PHP question on encoding (latin1_swedish_ci vs utf8_unicode_ci)


I have a question regarding encodings. In my MySQL database I have encoded users in latin1_swedish_ci (bad idea). Now I connect my iPhone application to Facebook and retrieve the list of friend names, and I would like to see if I already have these friends in my application. Sadly when I try to do the query I get this error:

#1267 - Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_unicode_ci,COERCIBLE) for operation 'like' 

So basically I try to mix the Facebook friend's name (in utf8) with the names in my dabatase (latin1).

I don't want to migrate the whole database to utf8, I just would like to know if it's possible in PHP to check if the name encoding is compatible with latin1, or at least if it's possible to encode all the names in latin1 and do the query, and if not just skip the name.

Does anyone of you know if it's possible to do something like this? Thanks,

Masiar


Solution

  • Not fully sure how are you looking for your friends in your application - via a search box or something? What is the role of the PHP script?

    But I think you can solve the issue by specifying character set and collation at the time of connecting to your database. By the way, I assume you are using MySQL.

    Run the following 3 queries each time you establish a DB connection, though I think the first 2 might suffice for you.

    SET character_set_connection = utf8
    SET character_set_client = utf8
    SET character_set_results = utf8