Jump to content

PDO::__construct(): Server sent charset (255) unknown to the client


Digger

Recommended Posts

My web host has upgraded the server to PHP8.1 and MySql8 2 days ago and i am constantly getting this error

 

 PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers in /home/***/***/app/Helpers/Database.php on line 63

This was line 63

$instance = new self("$type:host=$host;dbname=$name;charset=utf8", $user, $pass);

 

and this is what i was advised to change it

$instance = new self("$type:host=$host;dbname=$name;default-character-set=utf8", $user, $pass);

Sadly the error still persists

Here in the class function its in

 public static function get($group = false)
    {
        // Determining if exists or it's not empty, then use default group defined in config
        $group = !$group ?  [
            'type' => DB_TYPE,
            'host' => DB_HOST,
            'name' => DB_NAME,
            'user' => DB_USER,
            'pass' => DB_PASS,
        ] : $group;

        // Group information
        $type = $group['type'];
        $host = $group['host'];
        $name = $group['name'];
        $user = $group['user'];
        $pass = $group['pass'];

        // ID for database based on the group information
        $id = "$type.$host.$name.$user.$pass";

        // Checking if the same
        if (isset(self::$instances[$id])) {
            return self::$instances[$id];
        }

        try {
            // I've run into problem where
            // SET NAMES "UTF8" not working on some hostings.
            // Specifiying charset in DSN fixes the charset problem perfectly!
            $instance = new self("$type:host=$host;dbname=$name;charset=utf8", $user, $pass);
            $instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            // Setting Database into $instances to avoid duplication
            self::$instances[$id] = $instance;

            return $instance;
        } catch (PDOException $e) {
            //in the event of an error record the error to ErrorLog.html
            Logger::newMessage($e);
            Logger::customErrorMsg();
        }
    }

Its worked fine for the past 8 years until 2 days ago but i am now pulling mu hair out

 

Any help or advice would be greatly appreciated

 

 

Link to comment
Share on other sites

upon searching, this is due to the MySql server reporting a value back to the client (php in this case) that php doesn't recognize for the character set setting.

is this just an error that is getting logged or does it prevent the connection from being used?

here's a bug report that suggests a work-around of setting a character-set-server value in the MySql's my.cnf - https://bugs.mysql.com/bug.php?id=85946

Link to comment
Share on other sites

From a little searching, this seems like something that was a compatibility issue between older PHP versions and newer Mysql versions.  Are you sure your code has been updated to run on PHP 8?  Shouldn't be an issue on PHP 8 as far as I can tell.

Unless you need to do something with your hosting control panel to update your PHP version, this is something that you might have to get your host's support involved in to get fixed.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.