1

I am trying to access a localhost database via PHP and MySQL. Every time I try, I get the error: Warning: PDO::__construct(): Server sent charset (255) unknown to the client. Within the error message I see that I am calling: PDO->__construct('mysql:host=localhost;dbname=db;charset=utf8mb4', 'noah', 'mypassword'). Everything that I've looked up so far has told me that the problem has to do the character sets not matching between my call and the database. However, when I log into MySQL via mysql -u noah -p, do USE db and then SELECT @@character_set_database, @@collation_database; I see:

+--------------------------+----------------------+
| @@character_set_database | @@collation_database |
+--------------------------+----------------------+
| utf8mb4                  | utf8mb4_unicode_ci   |
+--------------------------+----------------------+

So they are using the same character set and I really don't know why the query isn't working. To make things weirder, when I change the database user or password that the constructor is called with, I get the same error. I would expect it to give me a different error about bad login credentials. But if I change the dsn, it does give me a different error about how it can't find the database.

I am using MySQL 8.0.17 and running the code via a local development server with google app engine. That dev server is running PHP 5.5.26

EDIT: Here is some of the actual code at the source of the problem:

$app['db'] = function (Container $c) {
    $pdo = new ExtendedPDO(
        $c['service.config']->get('db.dsn'),
        $c['service.config']->get('db.user'),
        $c['service.config']->get('db.password')
    );

Container is a Pimple container. App is a Silex application. ExtendedPDO is just a class that has extended PDO in ways that are unimportant here (it just keeps track of a query count while calling parent functions). All the db fields are grabbed from a env_dev.yaml file, and I know this to be true because when I change them there it changes the output of the error message. The error message traces back to the last line of the code block posted above, because on trying to create the PDO object it releases the exception.

Noah K
  • 77
  • 1
  • 8
  • Could you please show your actual code, ie `$someVar = new PDO(...` – Phil Oct 01 '19 at 00:54
  • Also, are you absolutely sure you're using PHP 7.3? Is your script running via the CLI or a web server (eg php-fpm)? If the latter, it's not uncommon to find out you've got multiple copies of PHP and your web server runs an older version. – Phil Oct 01 '19 at 00:59
  • 1
    @Phil I am pretty sure I'm correct about the version of PHP. I am running my script through the CLI and I made sure to update my $path so that it uses the newest version. However, I did notice earlier today that I had two versions of PHP installed (I didn't think it would ever use the old one if I didn't tell it to) so I'll try deleting the old one completely and see if that solves it. – Noah K Oct 01 '19 at 06:05
  • @Phil I've also added a tiny bit of code context. I can try to provide more if you want but this codebase I've inherited is a bit of a maze. – Noah K Oct 01 '19 at 06:24
  • Where are you setting the connection charset? Is that in the `db.dsn` property? Can you debug the value returned from `$c['service.config']->get('db.dsn')`? – Phil Oct 01 '19 at 06:34
  • _"I am running my script through the CLI"_ So to run this app, you're executing [`php -S localhost...`](https://silex.symfony.com/doc/2.0/web_servers.html#php)? If so, run `php -v` to check the version. If you're using a different [webserver configuration](https://silex.symfony.com/doc/2.0/web_servers.html#webserver-configuration), please give details – Phil Oct 01 '19 at 06:39
  • 1
    To run and build the app I'm using google app engine, and running a local dev server using the `dev_appserver.py` script provided with the google cloud sdk. When I run `php - v` it says 7.3.8 (cli). BUT it looks like within my composer.json, it sets the config.platform php version to 5.6. So that seems very likely to be the problem. Is there a way to work around this in php 5.6? Or do I just have to update? My fear is that if I upgrade I'll break something else within the project, but if I have to update I will. – Noah K Oct 01 '19 at 16:26
  • Also when I echo `$c['service.config']->get('db.dsn');` right before the PDO contructor, it spits out the expected `mysql:host=localhost;dbname=db;charset=utf8mb4` – Noah K Oct 01 '19 at 18:10

1 Answers1

1

Figured it out. PHP 5.0 doesn't seem to be able to read the responses from MySQL 8.0, no matter what I do. In my case, I uninstalled MySQL and reinstalled with version 5.7.27 and it works fine now.

Huge thanks to Phil for helping me figure it out.

Noah K
  • 77
  • 1
  • 8
  • Consider of updating the `my.cnf` file as below for MySQL 8.0. ```[client] default-character-set=utf8mb4 [mysql] default-character-set=utf8mb4 [mysqld] character-set-server=utf8mb4 collation-server = utf8mb4_unicode_ci default_authentication_plugin= mysql_native_password ``` – zhongxiao37 Feb 09 '23 at 02:05