S
S
superman19842016-02-11 15:51:09
PHP
superman1984, 2016-02-11 15:51:09

How to fix encoding in PHP?

Hello!
I run the code below (displays a record from the database on the page), hieroglyphs appear.
Hosting nic.ru
Connection encoding mapping: utf8mb4_general_ci (phpmyadmin main page)
Encoding in the database table cp1251_general_ci

<?php

$host = 'myLogin.mysql';
$database = 'myLogin_test';
$user = 'myLogin_mysql';
$pswd = 'password';

$dbh =  mysqli_connect ($host, $user, $pswd) or die ("No connect to server: " . mysql_error());
mysql_select_db ($database) or die ("No connect do db: " . mysql_error());

$query = "SELECT * FROM `my_sql_table`";
$res = mysql_query($query);
while($row = mysql_fetch_array($res))
{
    echo "Номер: ".$row['id']."<br>\n";
    echo "Имя: ".$row['firstname']."<br>\n";
    echo "Фамилия: ".$row['surname']."<br><hr>\n";
}
?>

output:
Номер: 1
РјСЏ: E
Фамилия: B
What to add to the code to make everything beautiful?
Thanks in advance!
<html>
        <head>
             <title>Привет!</title>
             <META charset='UTF-8'>
        </head>
                     <body>Номер: 1<br>
РРјСЏ: E<br>
Фамилия: B<br>

                     </body>
                     </html>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Cat Anton, 2016-02-11
@superman1984

A Guide to UTF-8 for PHP and MySQL

E
Evgeny Svirsky, 2016-02-11
@e_svirsky

First you need to add to the html
head.
And don't forget that the file must be UTF-8 without BOM. The base encoding has nothing to do with it.

A
Alexander, 2016-02-11
@kentuck1213

try specifying the encoding in the .htaccess file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question