V
V
Vladimir Homenok2015-08-06 15:03:23
PHP
Vladimir Homenok, 2015-08-06 15:03:23

How to choose the encoding in which MySQL will return strings?

Wrote a simple page today, loading strings from SQL. Everything works on the locale, but when I transferred it to the hosting, SQL began to return ASCII strings instead of UTF-8 strings => all Russian characters became "?".

<?php

include('configuration.php');

function sql_connect() {
    global $SQL_ADDRESS, $SQL_LOGIN, $SQL_PASSWORD, $SQL_BASE_NAME;
    mysql_connect($SQL_ADDRESS, $SQL_LOGIN, $SQL_PASSWORD) or die(mysql_error());
    mysql_select_db($SQL_BASE_NAME) or die(mysql_error());
    mysql_query("SET CHARACTER SET 'utf-8'");
    mysql_query("SET NAMES 'utf-8'");
    mysql_query("SET SESSION collation_connection = 'utf8_general_ci';");
};


sql_connect();
$query = 'SHOW VARIABLES LIKE "character_set_database"';
$result = mysql_query($query);
$r = mysql_fetch_array($result);
echo $r[1];
echo mb_detect_encoding($r[1]);
echo 'Русский пашет?';
?>

This code produces the following - utf8ASCII Russian plows? .
That is, the DB encoding is utf-8, the site is utf-8, but the strings are returned in ASCII.
It is worth adding that all records in the database are displayed normally.
If something is completely stupid - do not swear: 3

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vista1x, 2015-08-06
@MrHamster

Did you create the database immediately in UTF-8 encoding, or did you create it and then change the encoding? It may be that the fields you output have the encoding just cp1251, while the database itself is in UTF-8. Go to edit table fields, the encoding for each of the fields is indicated there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question