M
M
mix-92017-06-15 23:37:26
PHP
mix-9, 2017-06-15 23:37:26

How to configure unserialize for data from mysql?

Good day. The apache server and mysql were configured to work with win-1251, then the settings were changed to utf-8.

spoiler

mbstring.func_overload 2
mbstring.internal_encoding UTF-8
----
SET NAMES 'utf8
SET collation_connection = "utf8_unicode_ci"

unserialize(serialize("Russian text")); // "Russian text"
works correctly, but returns false for data received from mysql. Bitrix code, but it hardly matters
$data = \Bitrix\Main\Application::getConnection()->query('select * from b_event')->fetchRaw();
var_dump(unserialize($data['C_FIELDS'])); // false
What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sekira, 2017-06-15
@Sekira

Encoding problem. How were the settings changed from win-1251 to utf-8? The iconv php function will help you, var_dump(unserialize(iconv("UTF-8", "windows-1251", $data['C_FIELDS']))); (If it doesn't work, try swapping encodings).
But this is a bad option, you need to recode the table normally, then you won’t have to change anything in the code. In addition, if new data is entered into the table, they will already be in utf-8 encoding, and the old data will remain in windows-1251 and in general it will turn out to be a mess. So it is necessary to change the encoding of the entire table / database, more precisely each of its fields / lines. Since before that you entered data there in one encoding, and now you are reading the same data, but specifying a different encoding, although the data remained in the old encoding.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question