开发者

MySQL collation

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?


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..


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜