1

I have problems with selecting cyrillic text from the database. I get some undefined characters instead of letters. I've been searching the internet for the whole day but nothing.

Connection function:

protected function pdo_connect(){
    $instance = new PDO('mysql:dbname='.self::SERVER_DB_DATABASE.';host='.self::SERVER_DB_HOST.';charset=utf8', self::SERVER_DB_USER, self::SERVER_DB_PASS);
    $instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $instance->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');
    return $instance;
}

And selecting the text from the database:

$temp=array();
$stmt=$this->db->query("SELECT * FROM table");
$result=$stmt->fetchAll(PDO::FETCH_ASSOC);
for($i=0; $i < count($result); $i++){ 
    $temp[$result[$i]['text_index']]['en']=$result[$i]['en'];
    $temp[$result[$i]['text_index']]['sr_lat']=$result[$i]['sr_lat'];
    $temp[$result[$i]['text_index']]['sr_cyr']=$result[$i]['sr_cyr'];
}
return $temp;

And when I select the data from the database I get something like this: enter image description here As you can see, the cyrillic text isn't displayed properly but it's properly displayed in the database: enter image description here

And to mention that I have headers set:

header('Content-Type: text/html; charset=UTF-8');

So what is the problem here?

valek
  • 1,365
  • 16
  • 27
  • 1
    Take a look here http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Mihai Jan 25 '15 at 20:34
  • @Mihai Still nothing. I changed character set on all tables and columns, changed connection method and charset in header but I still get undefined symbols. – valek Jan 25 '15 at 20:43
  • Do you have this `` in your `` tag? – Mihai Jan 25 '15 at 20:49
  • @Mihai Yes, I tried with and without that (in combination with php header(charset)) but again nothing. – valek Jan 25 '15 at 20:53
  • Any change if you remove charset=utf8' from PDO? – Mihai Jan 25 '15 at 21:00
  • 1
    @Mihai Oh... I've just seen what I had done. I've been doing var_dump() for the whole time on the config.php page instead on the index.php so the charset was always under the output. I added some things and it's fine now. However, thank you very much for the link with utf8mb4 charset, I think it'll help me a lot. – valek Jan 25 '15 at 21:08

1 Answers1

1

Try correct your connection code to:

protected function pdo_connect(){
    $instance = new PDO('mysql:dbname='.self::SERVER_DB_DATABASE.';host='.self::SERVER_DB_HOST.';charset=utf8', self::SERVER_DB_USER, self::SERVER_DB_PASS);
    $instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $instance->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8; SET CHARACTER SET utf8; SET SESSION collation_connection = utf8_general_ci;');
    return $instance;
}
Alex
  • 137
  • 6