Characters not displaying correctly on a UTF-8 web

2019-04-30 13:49发布

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:

enter image description here

But on the site it's:

Nouveaux R�alistes

Here's everything I've checked...

The database is set to UTF-8:

enter image description here

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

enter image description here

The page header declares UTF-8:

enter image description here

The meta charset is set to UTF-8:

enter image description here

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

enter image description here

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

enter image description here

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>

2条回答
甜甜的少女心
2楼-- · 2019-04-30 14:50

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

查看更多
老娘就宠你
3楼-- · 2019-04-30 14:51

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.

查看更多
登录 后发表回答