H
H
hrvasiliy2015-11-03 12:26:04
PHP
hrvasiliy, 2015-11-03 12:26:04

What's wrong with encoding?

The database is set to utf-8 encoding, the site is the same. I write down in a DB from a text field the data in Russian. I see unreadable characters in the database. Tried to add before executing the query:

mysqli_query($mysqli, "SET NAMES 'utf8' COLLATE 'utf8_general_ci'");

Everything works fine, only I thought that this is an extra request, respectively, an extra load. I read that this is due to the fact that the encoding on the site does not match the encoding in the database, but this is not the case.
Is there another way to solve this problem? Previously, I did not have such problems, maybe because there was apache?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
neol, 2015-11-03
@hrvasiliy

By default mysql server uses latin1 encoding.
If I'm not confusing anything, then the request that came from you in utf8 is converted to latin1, and then the resulting abracadabra (normally utf8 is not converted to latin1) is converted back to utf8. The output is garbage.
Adding to my.cnf

collation-server = utf8_general_ci
character-set-server = utf8

followed by restarting mysql is supposed to solve the problem.
Well, or you can add to the same my.cnf
But in general it is worth leaving the line you provided. The load from such a request can be safely neglected, and the portability of the code will be slightly higher.

M
Max, 2015-11-03
@MaxDukov

there is a site encoding, a database encoding, and a client encoding. What do you have written in my.ini?
sorry, in my.cnf, in [client]

R
Ruslan Fedoseev, 2015-11-03
@martin74ua

the encoding is usually set immediately after the connection to the database...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question