Connection to the database is made as follows:

"mysql:host=**;dbname=**;charset=utf8" 

In the php file is written:

 setlocale(LC_ALL, 'utf8'); 

Data in the database:

 utf8-general-сi 

Still, the data obtained from the database (Russian language) are displayed as question marks.

  • What is the encoding of the database itself? - user197988
  • utf8-general-ci - Alexey
  • Type innoDB in the fields, too, utf8-general-ci - Alexey
  • and how the data get to the database? same script? - Maxim Tronenko
  • No, the data were already in the database. The base is taken from an old project just to study PDO - Alexey

2 answers 2

I figured it out myself, so:

Connect with the base

 $this->connect = PDO("mysql:host=;dbname=;charset=utf8") 

And then:

 $this->connect->query('SET NAMES utf8'); 

I didn’t find it anywhere in the documentation; maybe the method is not correct, but it all worked for me.

    You desperately need to upgrade.

    Setting the encoding in DSN is supported with PHP 5.3.6. And this version, for a minute, is already 5 years old, and it has not been supported for a long time.

    Setting the encoding with the SET NAMES query is a crutch, since this method does not change the internal encoding of the client, and the formatting of strings is done without regard to encoding. This situation should be avoided because in rare cases it can lead to injections.

    Setting the encoding in DSN is the only correct option.
    And working on an unsupported version of PHP faces many problems, from security to incompatibility.

    • So then they would suggest a solution to the problem !!! - Alexey
    • Setting the encoding with the SET NAMES query is a crutch, what other options are there? - Alexey
    • You desperately need to upgrade - what did you mean to upgrade? - Alexey
    • I had in mind the version of PHP. - Ipatiev
    • one
      @ Alexey in general this behavior is typical, as they wrote, for PHP versions up to 5.3.6. Do you really 5.6 behave this way? can i see phpinfo? - Maxim Tronenko