0

使用此代码时出现 UTF-8 编码错误:

$bdd->query("SET NAMES 'utf8'");

$id = $_SESSION['userid'];
$zero = 0;

$query_count = $bdd->query("SELECT count(*) FROM notification WHERE id_proprio = :id AND readed = :zero ORDER BY id DESC LIMIT 0,5",array("id"=>$id,"zero"=>$zero));
$count = $query_count[0]['count(*)'];
if($count >= 1)
{
  ?>

    <?php
    $query_notification = $bdd->query("SELECT * FROM notification WHERE id_proprio = :id AND readed = :zero ORDER BY id DESC LIMIT 0,5",array("id"=>$id,"zero"=>$zero));
    foreach($query_notification as $row)
    {
      $id = $row['id_exp'];
      $bdd->bind("id_exp", $id);
      $user_exp = $bdd->query("SELECT * FROM users WHERE id = :id_exp");
      ?>
      <li id="notif<?php echo $row['id']; ?>" 
        onMouseOver="this.style.backgroundColor='#f0f0f0'"
        onMouseOut="this.style.backgroundColor='#ffffff'"
        onclick="document.location.href='../<?php echo $row['link']; ?>'"
        style="cursor: pointer;">

        <div class="media" style="cursor: pointer;">
              <?php 
                if(!empty($user_exp[0]['avatar']))
                {
                  ?>
                    <a class="pull-left" href="#">
                      <img src="<?php echo base64_decode($user_exp[0]['avatar']) ?>" class="img-polaroid" style="max-width: 30px;margin-right:5px;">
                    </a>
                  <?php
                }
              ?>
              <div class="media-body" style="margin-left:5px;" style="cursor: pointer;" >
                <h5 class="media-heading" style="font-weight: 500;cursor: pointer;"><a style="color:grey;text-decoration:none;cursor:pointer;" onclick="supprimerNotif(<?php echo $row['id']; ?>,<?php echo $row['id_proprio']; ?>)"></h5></a>
                <p style="cursor: pointer;"> <?php echo tronque(utf8_decode($row['contenu']),50); ?></p>

                   <!-- <a href="../check/voirSource.php?id=<?php echo $row['id']; ?>" class="birne_unselectable btn btn-primary"><i class="icon-white icon-eye-open"></i></a> 
                    <a class="birne_unselectable btn btn-danger"><i class="icon-white icon-eye-close"></i></a>
                -->
              </div>
            </div>
        </li>
        <li class="divider"></li>
        <?php
    } ?>

  <?php
}else
{
  $id = intval($_SESSION['userid']);
  $un = 1;
  $query_notification = $bdd->query("SELECT * FROM notification WHERE id_proprio = :id AND readed = :un ORDER BY id DESC LIMIT 0,5",array("id"=>$id,"un"=>$un));

  $query_count = $bdd->query("SELECT count(*) FROM notification WHERE id_proprio = :id AND readed = :un ORDER BY id DESC LIMIT 0,5",array("id"=>$id,"un"=>$un));      
  $count = $query_count[0]['count(*)'];
  if($count == 0)
  {
    echo "Aucune activité récente de vos amis";
  }
  foreach($query_notification as $row)
    {

      $id = $row['id_exp'];
      $bdd->bind("id_exp", $id);
      $user_exp = $bdd->query("SELECT * FROM users WHERE id = :id_exp");
      ?>
      <li id="notif<?php echo $row['id']; ?>" 
        onMouseOver="this.style.backgroundColor='#f0f0f0'"
        onMouseOut="this.style.backgroundColor='#ffffff'"
        onclick="document.location.href='../<?php echo $row['link']; ?>'"
        style="cursor: pointer;">
        <div class="media">
              <?php 
                if(!empty($user_exp[0]['avatar']))
                {
                  ?>
                    <a class="pull-left" href="#">
                      <img src="<?php echo base64_decode($user_exp[0]['avatar']) ?>" class="img-polaroid" style="max-width: 30px;margin-right:5px;">
                    </a>
                  <?php
                }
              ?>
              <div class="media-body" style="margin-left:5px;">
                <h5 class="media-heading" style="font-weight: 500;"><a style="color:grey;text-decoration:none;cursor:pointer;" onclick="supprimerNotif(<?php echo $row['id']; ?>,<?php echo $row['id_proprio']; ?>)"></h5></a>
                <p> <?php echo tronque(utf8_decode($row['contenu']),50); ?></p>

                   <!-- <a href="../check/voirSource.php?id=<?php echo $row['id']; ?>" class="birne_unselectable btn btn-primary"><i class="icon-white icon-eye-open"></i></a> 
                    <a class="birne_unselectable btn btn-danger"><i class="icon-white icon-eye-close"></i></a>
                -->
              </div>
            </div>
        </li>
        <li class="divider"></li>
        <?php
    } 
}

函数 tronque() 对字符串进行 troncating,我们使用 MySQL POO API,它是 $bdd。

此代码返回以下带有“?”的字符串 而不是例外的“é”,例如:

机器人马克西姆评论投票出版物

如何纠正这个编码问题?感谢您未来的帮助!

4

1 回答 1

-1

您可能希望在响应标头中将 UTF8 设置为字符集

header("Content-Type: text/html; charset=utf-8");


或者还有一个met标签(HTML5的例子):

<meta charset="UTF-8">
于 2013-11-09T18:06:26.937 回答