Link to home
Start Free TrialLog in
Avatar of digital0iced0
digital0iced0

asked on

mysql_field_len() with utf-8 = incorrect field length

Hello,

Can anyone tell me a way to retrieve the correct field length from a field?  What is happening now is when the field is utf-8, mysql_field_len() returns the field length times three.  So say a field has a 20 character limit, it will return 60.  Right now I'm dividing the output by three but I'm guessing the script would not function properly if the table had a different collation.

Any help or alternatives would be appreciated.
Avatar of cr4ck3rj4ck
cr4ck3rj4ck
Flag of United Kingdom of Great Britain and Northern Ireland image

Hey there,

It seems that this is a recorded MySQL problem.

Please see: http://bugs.php.net/bug.php?id=33123

You could get the field data in to a PHP variable and use strlen() as an alternative.

Hope that helps,
CJ
@digital0iced0: Check this:
http://lists.mysql.com/mysql/183792

Best, ~Ray
Avatar of digital0iced0
digital0iced0

ASKER

Thanks for the replies.  The problem is that I'm looking for the maximum allowed field length not the specific length of a string.  I need the 40 from varchar(40).  But like cr4ck3rj4ck mentioned its a bug that it returns the total bytes allowed instead of the character limit.  I need to get this information dynamically depending on the table, in other words I don't want to hard code 40 into the script because its supposed to work with multiple tables.  Can you guys think of an alternative way of achieving this?

Thanks,

Alex
Yes, I'll show you one...
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot Ray, you really are a genius :)
Thanks for your points and the kind words, as well as your many thoughtful contributions to EE.  This is a great question as more and more people use UTF-8 and I'm glad its in our archives.  Best always, ~Ray