K
K
Kirill Firsov2013-11-18 22:41:27
PHP
Kirill Firsov, 2013-11-18 22:41:27

Why do Russian characters disappear in MySQL INSERT?

All mysql encoding is utf8.
I'm trying to load a text file in ANSI encoding (as notepad++ says)
Reading lines and loading into a column with utf8_general_ci encoding.
The problem is that Russian characters disappear.
Users with utf8 and ANSI encoding will upload files, so the problem needs to be solved somehow.
Doing iconv for each line is too expensive, so another solution must be found.
UPD:
Now I did it like this:

$data = [];
      if ($handle = fopen($_FILES['file']['tmp_name'], "r")) {
        while (!feof($handle)) {
          $line = fgets($handle, 4096);
          $line = trim($line);
          $encode = mb_detect_encoding($line, ['UTF-8'], true); // false если русские символы
          if ($encode === false)
          {
            $line = iconv("windows-1251", 'utf-8', $line);
          }
          if (!empty($line))
          {
            $data[] = [
              'good_id' => $goodId,
              'data' => $line
            ];
          }
        }
        fclose($handle);
      }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2013-11-18
@FanatPHP

First, "too expensive" is a fantasy.
Secondly, when loading data into the database, you need to tell it what encoding it is in.
Thirdly, the encoding has nothing to do with it. If there are problems with the encoding, the text may either look like question marks or unreadable characters. But don't disappear completely.
In general, without knowing any details at all, it is difficult to give specific recommendations, but if we assume that an INSERT query and mysql_query () are used, then after connecting to the database, you need to write
mysql_set_charset('кодировка');
where the encoding is utf8 or cp1251, depending on the source encoding of the file.
For other insertion methods, you may need other ways to specify the encoding.
If the characters continue to disappear - it's not in the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question