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)