Issue in MySQL character limit
I 开发者_开发知识库have a VARCHAR field in one of the tables in my MySQL database. It has a limit of 200 characters. This works fine in English, but when I enter 200 Arabic characters (UTF-8), only 110 characters get stored.
Any idea why this is happening and how I can solve it?
This might be caused by the connection or client character sets being incorrectly set.
Issue SHOW VARIABLES LIKE 'character_set%'
and see what the values of character_set_connection
and character_set_client
are. If they are latin1
, you need to make sure that you change your code to get new connections to use UTF-8. This can be achieved by issuing SET NAMES 'utf8'
when you have connected. (Your database abstraction class may provide a method for changing the connection character set, in which case you should do that instead.)
If this is indeed the problem, you may have stored mis-encoded data in your tables, which you will want to fix.
The way MYSQL makes keys and looks at UTF-8 is your problem. It assumes any UTF-8 character will be 3 bytes (for worst-case calculations), so will limit your size without really explaining why. More info:
http://www.xaprb.com/blog/2006/04/17/max-key-length-in-mysql/
Thanks alot guys, I've learned alot from your answers. However, I've found that what was causing the problem isn't MySQL, but PHP. The substring function for PHP doesn't play nicely with Arabic strings, and cut them off early, so I just had to remove it.
精彩评论