Answer the question
In order to leave comments, you need to log in
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 'Русский пашет?';
?>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question