Characters not displaying correctly on a UTF-8 website

佐手、 提交于 2019-12-12 09:39:29

问题


I've done everything I can think of, but special characters are not displaying correctly on this webpage.

For example, in the database it's:

But on the site it's:

Nouveaux R�alistes

Here's everything I've checked...

The database is set to UTF-8:

The page was written in NetBeans, with the document encoding set to UTF-8:

The page header declares UTF-8:

The meta charset is set to UTF-8:

I've even added the following line to my .htacess:

But there characters are still not displaying correctly, and I get the following error from the W3C Validator:

I feel like I've attempted everything, but it still won't work. (I've even tried htmlspecialchars and htmlentities in PHP, but the page doesn't even render!)


UPDATE
As requested, here is the code I'm using:

class Exhibition {
    public $exhibitionDetails;    

    public function __construct(Database $db, $exhibitionID){
        $this->_db = $db;

        $params['ExhibitionID'] = $exhibitionID;

        $STH = $this->_db->prepare("SELECT * 
            FROM Exhibition
            INNER JOIN Schedule
                ON Exhibition.ExhibitionID = Schedule.ExhibitionID            
            WHERE Schedule.Visible = 1
                AND Exhibition.ExhibitionID = :ExhibitionID;");

        $STH->execute($params);

        $this->exhibitionDetails = $STH->fetchAll(PDO::FETCH_ASSOC);

    }
}

And...

try {
    $db = new Database(SITE_ROOT."/../");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $exhibition = new Exhibition($db,$_GET['id']);
} catch (PDOException $e) {
    echo "<p class='error'>ERROR: ".$e->getMessage()."</p>";
}

And finally...

<p><?php echo $exhibition->exhibitionDetails[0]["Desc"]; ?></p>

回答1:


If you are using mysql_* functions:

mysql_query("SET NAMES 'utf8'");

If you are using PDO

$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'username';
$password = 'password';
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
); 

$dbh = new PDO($dsn, $username, $password, $options);

It sets connection encoding.




回答2:


It's been a few years since I've used PHP but back then it didn't natively support Unicode and a quick search of google tells me it still doesn't. You can still make it work though.

Here's a great link:

Encodings And Character Sets To Work With Text



来源:https://stackoverflow.com/questions/16092448/characters-not-displaying-correctly-on-a-utf-8-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!