0

I got an encoding error with UTF-8 with this code :

$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
    } 
}

The function tronque() is troncating the string, We use a MySQL POO API who is $bdd.

This code returned the following string with a "?" and not the excepted "é", e.g. :

Robot Maxime a comment� votre publication

How to correct this encoding problem ? Thanks for future help!

PeeHaa
  • 71,436
  • 58
  • 190
  • 262

1 Answers1

-1

You might want to set UTF8 as charset in the response headers

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


Or also a met tag (example for HTML5):

<meta charset="UTF-8">
Maru
  • 894
  • 1
  • 12
  • 29