1

I'm having troule displaying correctly some informations from my database. When i try to echo without using the function, characters like accents and stuff are replaced with black blocks and question marks. If i try to use htmlspecialchars, i doesn't display anything and i can't find out why.

Here is my code:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
<?php

try
{ 
$bdd = new PDO("mysql:host=localhost;dbname=myblog","root",""); 
}
catch (Exception $e)
{
die("Erreur : " . $e->getMessage());
} 

$sql1="SELECT * FROM article ORDER BY date_creation";
$reponse1 = $bdd->query($sql1);   


?>


<?php while ($data1 = $reponse1->fetch()) {  ?>

    <table> 
        
            <tr>

           <?php echo htmlspecialchars($data1['titre']) ;  ?>

            </tr>
        
        
        
        
        
        
            </table>


   <?php }   ?> 



Didimaox
  • 41
  • 3
  • Are you storing multi language database? – KHIMAJI VALUKIYA Dec 16 '21 at 15:42
  • Looks like a problem of charsets. Check how your html is set to use UTF-8 or non-UTF8 chartset. Look for ``in your `` section. If it is set then your database tables have collations non-utf8 and then, when showing them in browser, looks damaged. Take a look [here](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) too. – masterguru Dec 16 '21 at 15:49
  • Does this answer your question? [UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – ADyson Dec 16 '21 at 18:54
  • @KHIMAJIVALUKIYA no i'm not, at least i don't think so haha – Didimaox Dec 17 '21 at 07:32

1 Answers1

1

I fixed my problem doing this change on my PDO:

Instead of:

$bdd = new PDO("mysql:host=localhost;dbname=myblog","root","");

i tried:

$bdd = new PDO('mysql:host=localhost;dbname=myblog;charset=utf8', 'root', '', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

and it worked!

Saman Salehi
  • 1,004
  • 1
  • 12
  • 19
Didimaox
  • 41
  • 3