I
I
Ilya2016-11-01 10:15:11
PHP
Ilya, 2016-11-01 10:15:11

Why does the encoding get lost when writing to MYSQL?

Hello, there is a news parsing page using html dom parser. For more than 2 months everything was fine, until today the encoding magically got lost.
On the parsing page, everything is fine, as soon as the string enters the database, we get (Многодетные СЃР) crocozyabrs. Before that everything was fine. All pages with parsing and output are utf-8 encoded. utf-8-general-ci is set in the database. Who faced or who can suggest a solution?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JWprogrammer, 2019-06-01
@JWprogrammer

In 2019 mysql_query() has been deprecated. Use the same, only through mysqli_query().
Example:

$conn = mysqli_connect($servername, $username, $password, $database); //подключение к БД через mysqli_connect

mysqli_query($conn,"SET NAMES UTF8");
mysqli_query($conn,"SET CHARACTER SET UTF8");

I have this code fixed "crazy" when writing to the database (everything was fine on the page anyway).

I
Ilya, 2016-11-01
@ironfrol

Fixed everything, maybe it will be useful for someone. I don’t know why the encoding went wrong, but I solved the problem as follows, after connecting to the database I wrote two lines

mysql_query("SET NAMES UTF8");
mysql_query("SET CHARACTER SET UTF8");

Now the rows in the database are in normal encoding and the output on the page too.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question