字符在UTF-8网站上无法正确显示 [英] Characters not displaying correctly on a UTF-8 website

查看:99
本文介绍了字符在UTF-8网站上无法正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尽我所能,但是特殊字符无法在此网页上正确显示.

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

例如,在数据库中是:

但是在网站上是

Nouveaux R�alistes

这是我检查过的所有内容...

Here's everything I've checked...

数据库设置为UTF-8:

The database is set to UTF-8:

该页面是用NetBeans编写的,文档编码设置为UTF-8:

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

页面标题声明了UTF-8:

The page header declares UTF-8:

元字符集设置为UTF-8:

The meta charset is set to UTF-8:

我什至在.htacess中添加了以下行:

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

但是仍有字符无法正确显示,并且我从W3C验证程序收到以下错误:

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

我觉得我已经尝试了一切,但仍然无法正常工作. (我什至在PHP中都尝试过htmlspecialcharshtmlentities,但是页面甚至无法呈现!)

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);

    }
}

然后...

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>";
}

最后...

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

推荐答案

如果使用的是mysql_*函数:

mysql_query("SET NAMES 'utf8'");

如果您使用的是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);

它设置连接编码.

这篇关于字符在UTF-8网站上无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆