mysql delivering a 'Can't initialize character set utf-8 (path: /usr/share/mysql/charsets/)' error, no utf8.xml file there

14,483

did you try

if(!$db->set_charset('utf8')) {

without the dash?

since all your research on your system points to utf8 instead of utf-8 ;)

Share:
14,483

Related videos on Youtube

Iam Zesh
Author by

Iam Zesh

Updated on July 13, 2022

Comments

  • Iam Zesh
    Iam Zesh 6 months

    I am on the Path of learning more about mysqli and all that exciting stuff but I get blocked quite soon.

    I have a local server on my debian box. It is up to date, has php and mysql installed and running smoothly. I was looking to learn a bit more on mysqli and as I tried the following code:

    <?php
    
    $db = new mysqli('localhost', 'userdb', 'pwuserdb', 'db');
    
    if(!$db->set_charset('utf-8')) {
        printf("Error setting the character set utf-8: %s\n", $db->error);
    } else {
        printf("Current character set is: %s\n", $db->character_set_name());
    }
    
    print_r($db->get_charset());
    
    ?>
    

    I was, to my surprise, getting the following message, when visiting the page: Error setting the character set utf-8: Can't initialize character set utf-8 (path: /usr/share/mysql/charsets/) stdClass Object ( [charset] => latin1 [collation] => latin1_swedish_ci [dir] => [min_length] => 1 [max_length] => 1 [number] => 8 [state] => 801 [comment] => cp1252 West European )

    I thought to myself that it is logical as I didn't set up utf-8 as the standard charset of mysql so I completed with the following settings in the my.cnf file:

    for [mysqld]

    default-character-set=utf8
    

    for [client]

    default-character-set=utf8
    

    I also logged into mysql from the command line and ran ALTER DATABASE db CHARSET=utf8;

    I also reloaded mysql from the command line, as well as apache. When looking how things are going on in mysql, almost everything looks alright:

    mysql> SHOW VARIABLES LIKE 'character_set%';
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | utf8                       |
    | character_set_connection | utf8                       |
    | character_set_database   | utf8                       |
    | character_set_filesystem | binary                     |
    | character_set_results    | utf8                       |
    | character_set_server     | utf8                       |
    | character_set_system     | utf8                       |
    | character_sets_dir       | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+
    8 rows in set (0.00 sec)
    

    But due to the fact that it seems like mysql cannot locate the utf8 file, I checked for a utf8.xml file in the /usr/share/mysql/charsets/ folder and there isn't one. In the Index.xml file under this directory there is the mention of utf8, in the list of the charsets but I suppose that the problem comes from the fact that the xml file is missing in the directory.

    Just for the information, my system locales are all UTF8 (en and pl) and I cannot understand why the utf8.xml file is not in the directory, as I haven't been goofying around with this directory or its content at all.

    Any idea/ advice/ recommendation is welcome.

    Thank you in advance!

    Cheers!

  • Iam Zesh
    Iam Zesh over 10 years
    That was it!! Thanyk you very much!