George
George

Reputation: 2110

MySQL and PDO: two languages in one table using UTF-8 collation

Consider a table with three columns id,name and bgname where bgname is a cyrillic equivalent of name.The table is created with UTF-8 collation. After using the following:

<?php

    $sql = 'SELECT bgname FROM categories';
    function getZapisi($sql,$dbh) {
        foreach ($dbh->query($sql) as $row) {
            print $row['bgname'] . "<br/>";
        }
}
try {
    $dbh = new PDO("mysql:host=localhost;dbname=test", 'root', 'pass');
    /*** echo a message saying we have connected ***/
    getZapisi($sql,$dbh);
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }

?>

I get ??? from the query no matter if I use cp1251 or utf-8 collation for the bgname column. Thanks in advance

Upvotes: 0

Views: 304

Answers (3)

George
George

Reputation: 2110

In fact the problem was terminated using:

$dbh = new PDO("mysql:host=localhost;dbname=test;", 'root', 'pass');
$dbh -> exec("set names cp1251");

Thanks for the effort

Upvotes: 0

Alfabravo
Alfabravo

Reputation: 7589

Check the encoding of your page. maybe you're getting correct results but the output html created by apache+php tells the browser to use some other encoding.

Upvotes: 1

Related Questions