0

There is MySQL database with one table:

<?php

   $con=mysqli_connect("db2.ho.ua","spectorsky","s124816",spectorsky); 
   if (!$con) echo mysqli_connect_errno() . PHP_EOL.mysqli_connect_error() . PHP_EOL;    
   $sql="CREATE DATABASE if not exists spectorsky CHARACTER SET = utf8 COLLATE = utf8_unicode_ci";
   $mysql_result=mysqli_query($con,$sql);    
   if ($mysql_result) {
      echo "Database spectorsky created successfully". PHP_EOL;    }    else 
      echo "Database spectorsky hasn't been created: ".$con->errno.$con->error. PHP_EOL;    
   $sql="Create table if not exists categories (idCategory INTEGER PRIMARY KEY, nameCategory TEXT)".    " CHARACTER SET = utf8  COLLATE = utf8_unicode_ci";    
   if (mysqli_query($con,$sql)){
      echo "Table categories created successfully". PHP_EOL;    }    else
      echo "Table categories hasn't been created: ".mysqli_error($con). PHP_EOL;   
   mysqli_query($con,"delete from categories") or die(mysqli_error($con));   
   echo "That's all";   
   mysqli_close($con); 
?>

Text field of the table contains Cyrillic data in unicode charset. The table is created on Adroid device, then exported to and the imported from the server. After that, all strings on Android device are correct. However, when I try to see these strings on the server via phpmyadmin, they are displayed in cp1251 (string АБВ is displayed as АБВ). In the list of phpmyadmin variable list I see that the variable character set database is set to 1251, I I have no sufficient rules to edit it. Queries set names utf8 or set character_set_database to utf8 at SQL tab are performed with no error, but nothing changes. So, the question: is it possible to change character_set_database variable for the current session, or maybe there is other way to make phpmyadmin to display unicode in a correct way?

Thanks in advance

Spectorsky
  • 608
  • 4
  • 23

1 Answers1

1

Pick either cp1251 or utf8 (or utf8mb4), but don't mix them.

АБВ, encoded as utf8 is hex D090 D091 D092. Note: 2 bytes per Cyrillic character.

If you treate hex D0 90 D0 91 D0 92 as cp1251, you get АБВ. Note that is 6 bytes for 6 characters. Note the repetition of Р(Capital ER); this comes from D0.

For what causes it, see "Mojibake" in Trouble with UTF-8 characters; what I see is not what I stored - but replace "latin1" with "cp1251" as needed.

(After further info...)

Use this once, right after connecting:

mysqli_query($con, "SET NAMES utf8");

(Note to other readers: If you need Chinese or Emoji, everything should utf8mb4, not utf8.)

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • Rick James, thanks for reply. I didn't mix different encodings, I'd like to set everything to utf8. However, I haven't enough rights to set server variable `character_set_database` to utf8.... – Spectorsky Nov 25 '17 at 23:49
  • 1
    `mysqli_query($con, "SET NAMES utf8");` – Rick James Nov 26 '17 at 00:42
  • It works, thanks! It should be used when new lines are being inserted into table (not when creating the table). I think you should type it as an answer in order that I could upvote it. – Spectorsky Nov 26 '17 at 09:43
  • 1
    I added to my Answer. Do it just once. – Rick James Nov 26 '17 at 16:00