简体   繁体   中英

Characters not displaying correctly on a UTF-8 website

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>

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.

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

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