Search code examples
sqlcharacter-encodingmariadbcollation

Character set and collation all the way through


I am trying the apply the principle of start to end character set and collation, but I have come up against the server character set. I have set all php files and the database to UTF8MB4, with collation UTF8MB4_general_ci. All of the Greek and Maths symbols appear correctly on the database, both on localhost and local files BUT they do not render correctly on the server. They DO appear correctly on the server DB. When I run:

SHOW SESSION VARIABLES LIKE 'character\_set\_%';
SHOW SESSION VARIABLES LIKE 'collation\_%';

I get the following feedback:

character_set_client    utf8mb4
character_set_connection    utf8mb4
character_set_database  utf8mb4
character_set_filesystem    binary
character_set_results   utf8mb4
character_set_server    latin1
character_set_system    utf8

This looks to me like the server character set is causing the problem.

My issue is that I don't know how to set the server character set to UTF8MB4. The MariaDB documentation suggests that I should run SQL:

SET character_set_server = 'UTF8MB4';

Which I have done, but it has changed nothing. Where do I go from here?


Solution

  • After spending 3 days browsing the web I came upon the following solution. After establishing the connection

    $con = mysqli_connect("DB_host","USER_name","**********", "DB_Name");
    mysqli_set_charset($con, "utf8");
    

    I called:

    mysqli_query("SET NAMES 'utf8'");

    mysqli_query("SET CHARACTER SET 'utf8'");

    .. and all of the symbols magically appeared!

    Pure luck!