6

Today I had an issue today with PHP and MySQL (here is the question), which has already been solved but it awakened a new, more theorical, question.

The thing is that in MySQL database I had a field whose type was VARCHAR(64). When trying to insert a string from a php script, the full string was more than 64 bytes, making a var_dump proved it had 71. BUT, here is what I do not understand, running the exact same query, with the exact same string, but manually from phpmyadmin, it did fit in the 64-byte sql field.

So, here is the question. Is there any reason of why the same string in php takes more memory than inserting it directly to the database?

EDIT: Someone suggested a duplicate of a charset-related question. The charset in MySQL is utf8_general_ci. In PHP I have not defined the charset, but if I di print(mb_detect_encoding($string)); I get it is ASCII encoded. May this be the reason? How wrong can I be if I think ASCII takes less memory than utf8? (Anyways, in the script I have mysqli_set_charset($con,"utf8"); so it should be converted to utf8 before querying)

Community
  • 1
  • 1
pablito.aven
  • 1,135
  • 1
  • 11
  • 29
  • Are you sure PHPMyAdmin inserted the whole string and didn't just truncate a part of it (as it did when I just tried it)? – kero Feb 11 '16 at 20:37
  • 1
    don't confuse bytes with characters. a unicode string can have 64 "characters", but require many more bytes than 64. – Marc B Feb 11 '16 at 20:39
  • 1
    @kingkero chk link that OP mentioned in question – devpro Feb 11 '16 at 20:39
  • When I copy the URL in the previous question it has a hidden character in it which returns `string(65) "http://example.com/upload/news_img/1455220595Untitled-1‌​.jpg"` Removing the hidden character reduces the string length to 62. – Jay Blanchard Feb 11 '16 at 20:46

0 Answers0