We use cookies
We use cookies to optimize our website. By continuing to browse the site, you agree to our use of cookies.
Control panel
  • Русский
  • Українська
  • English
  • UAH
  • USD
  • RUB
  • EUR
  • 0-800-307-307 Hotline
  • +38 (044) 392-74-33 Kiev
  • +38 (057) 728-39-00 Kharkiv
  • +38 (056) 794-38-31 Dnipro
  • +38 (032) 229-58-93 Lviv
  • +38 (048) 738-57-70 Odessa
  • +38(093) 170-15-42  Life
  • +38 (067) 400-88-44 Kievstar
  • +1(888)393-24-51  USA, Toll free
  • +44(131)507-01-14  Great Britain
  • +7 (499) 348-28-61 Moscow

2.7.3.1. Invalid encoding when outputting data from the database

If the encoding of the site and the database does not match (some of the text on the site is displayed normally, and some of the text from the database is in the form of incomprehensible characters), you need to add commands in the script that connects to the database that will indicate the encoding to the MySQL server where you want to display the text. Depending on which PHP library you are using, the commands will look like this:

  • For the library mysql:
    mysql_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
    mysql_query("SET CHARACTER SET 'utf8'");
  • For the library mysqli:
    mysqli_query($link, "SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
    mysqli_query($link, "SET CHARACTER SET 'utf8'");

In commands:

  • Instead utf8 you must specify the desired encoding (for example, cp1251).
  • Instead utf8_general_ci - the desired encoding mapping (for example, cp1251_general_ci). A complete list of MySQL encodings and collations is available at official documentation.
  • For the library mysqli the first parameter specifies the pointer to the database connection - in your script it may be different from $link (you can find it out by looking in the source code for the name of the variable to which the result of the function is assigned mysqli_connect()).