I do a small test task in the Cloud9 cloud: you need to get a line from a csv-file in php, select everything before the first tabulation and put the result in the database. Everything works wonderfully, only krakozyabry enter the base ((Since I am not even a novice, but a senior kindergarten group, I cannot cope. Here’s the code:

<?php // такая русская локаль указана в /etc/locale.alias виртуальной машины, // но setlocale не работает setlocale(LC_ALL, "ru_RU.ISO-8859-5"); // аналогично не работает ( iconv_set_encoding("internal_encoding", "UTF-8"); iconv_set_encoding("input_encoding", "UTF-8"); iconv_set_encoding("output_encoding", "UTF-8"); // выдает ISO-8859-1 (несмотря на setlocale() вверху) echo mb_internal_encoding(); echo "\n"; $fp = fopen("test2.csv", "r"); $buffer = fgets($fp); $date = strstr($buffer, chr(9), true); // обе строки - UTF-8, проверял mb_detect_encoding() $link = mysql_connect("127.0.0.1", "user", "password"); $database = "db"; mysql_select_db($database); // у базы тоже кодировка UTF-8 if (!$link) { $error = mysql_error(); printf("Cannot connect to database\n$error\n"); } else { printf("Connected to database\n"); } $query = "INSERT INTO table(field) VALUES('$date')"; $result = mysql_query($query); if (!$result) { $error = mysql_error(); printf("Cannot insert into database\n$error\n"); } else { printf("Inserted into database\n"); } fclose($fp); ?> 

Ps

"And you turn it on - it does not work" (c). The line "July 12th" looks like this: 12 months

Yes, Cloud9 support has been silent for a day.

No, simply importing csv into mysql is impossible - only the first tab is needed (or rather, the line before it).

Yes, dpkg-reconfigure console-setup did (there is ubuntu in the virtual machine). No, it does not help.

In general, you have all the hope.

    2 answers 2

    Immediately after establishing the connection with MySQL DBMS, complete the query

     SET NAMES utf8 

    The fact is that MySQL converts incoming text from a client if it comes in a different encoding from the encoding of the database. For example, create a database in UTF-8, and send requests in the Windows-1251 encoding and MySQL will cope with the conversion of the encoding. However, for this to happen, you must either configure the server, or tell MySQL at the beginning of the session in what encoding you are going to send data.

    If you do not configure MySQL, the default connection encoding is latin1 (since MySQL is Sweden). Therefore, MySQL is trying to convert latin1 to UTF-8 (despite the fact that you give it UTF-8 text). She needs to be told explicitly that you are going to send text in UTF-8 encoding so that she doesn’t perform any unnecessary conversions.

    UPDATE

    Also, if you are not using anything other than UTF-8, delete the line

     setlocale(LC_ALL, "ru_RU.ISO-8859-5"); 
    • It did not help ((I tried after mysql_connect and after mysql_select_database. Code: $ utfquery = "SET NAMES utf8"; $ utf8 = mysql_query ($ utfquery); - Anton Foigt
    • @AntonFoigt, if it is not difficult to supplement the question with the appearance of krakozyabl - perhaps by them it will be possible to understand what is happening. Also, please tell us in what encoding is the test2.csv file? - cheops
    • ... besides, krakozyabry are born even before the connection with the base: checked $ date var_dump (just before mysql_connect) - in the line "July 12" it has 11 characters instead of 8 - Anton Foigt
    • @AntonFoigt Aha, do you have a script in which encoding? - cheops pm
    • "July 12th" looks like this: 12 test2.csv - in utf-8. the script displays everything correctly in the console (with the exception of the number of characters in Cyrillic lines), but it puts a dash in the baseAnton Foigt

    Everything worked when I inserted mb_internal_encoding("utf-8"); at the top of the script mb_internal_encoding("utf-8");

    I apologize for the concern - this is the first, but apparently not the only Nubian question))