V
V
Vladimir Homenok2015-08-06 12:44:59
PHP
Vladimir Homenok, 2015-08-06 12:44:59

How to make PHP and MySQL encodings compatible?

The essence of the problem is this: when pulling words from SQL into PHP, the words themselves are understood by PHP normally, but individual letters turn into scribbles:

// === Здесь идут запросы ===
$word = mysql_fetch_array($result);
echo $word; // Выведется, к примеру "бороды"
echo $word[1]; // Выведется "�"

All code in utf-8, at the beginning I tried to write "header("Content-Type: text/html; charset=utf-8");", I also specify the encoding before requests using SET CHARACTER SET and SET NAMES, encoding in the database utf8_general_ci.
What could be the problem? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2015-08-06
@MrHamster

The problem is that you don't understand that utf8 is a multi-byte encoding, and the letters of utf8 strings in php cannot be accessed via a serial number.
If you need the second letter it should be like this:

$word = 'борода';
echo $word[2].$word[3];

But it is preferable, of course, the normal version:
echo mb_substr($word, 1, 1, "UTF-8");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question