Reputation: 1376
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:
As you can see, the cyrillic text isn't displayed properly but it's properly displayed in the database:
And to mention that I have headers set:
header('Content-Type: text/html; charset=UTF-8');
So what is the problem here?
Upvotes: 1
Views: 1307
Reputation: 137
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;
}
Upvotes: 1