0

I am wondering if I were to validate a string that will be inserted into MySQL, do I need to consider single vs multi byte characters? I wonder if I can assume that a string that has a strlen() of 20 can be inserted into a MySQL varchar(20)?

Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
JM at Work
  • 2,417
  • 7
  • 33
  • 46

2 Answers2

2

In mysql you specify the number of characters (they can be multibyte), whereas strlen() measures the length of string supposing that char == byte.

So if your string strlen() == 20 then you always can insert it into varchar(20) since it is the same length or shorter actually (20 bytes <= 20 characters).

zerkms
  • 249,484
  • 69
  • 436
  • 539
1

It depends MySQL version and encoding of your column.

If using MySQL 4, it's number of bytes, and you can use strlen to count them.

If using MySQL 5, and using utf8 tables, it's number of chars, and then you need to use a multibyte aware function like mb_strlen to count chars in your string.

See MySQL VARCHAR Lengths and UTF-8 for more details.

Community
  • 1
  • 1
Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
  • "I wonder if I can assume that a string" --- i think that he doesn't need `mb_strlen` – zerkms Jun 09 '11 at 03:48
  • The same, ok ;-) Btw, good point about mysql4. I've almost already forgotten there is something other than mysql5.1 – zerkms Jun 09 '11 at 03:57