Tuesday 19 January 2016

UTF-8 problems PHP/MySQL



I've always used ISO-8859-1 encoding, but I'm now going over to UTF-8.




Unfortunately I can't get it to work.



My MySQL DB is UTF-8, my PHP document is encoded in UTF-8, I set a UTF-8 charset, but it still doesn't work.



(it is special characters like æ/ø/å that doesn't work)



Hope you guys can help!


Answer



Make sure the connection to your database is also using this character set:




$conn = mysql_connect($server, $username, $password);
mysql_set_charset("UTF8", $conn);


According to the documentation of mysql_set_charset at php.net:



Note:
This is the preferred way to change the charset. Using mysql_query() to execute
SET NAMES .. is not recommended.



See also: http://nl3.php.net/manual/en/function.mysql-set-charset.php



Check the character set of your current connection with:



echo mysql_client_encoding($conn);


See also: http://nl3.php.net/manual/en/function.mysql-client-encoding.php




If you have done these things and add weird characters to your table, you will see it is displayed correct.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...