简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM