Didimaox
Didimaox

Reputation: 33

Issue with Htmlspecialchars

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



Upvotes: 1

Views: 185

Answers (1)

Didimaox
Didimaox

Reputation: 33

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!

Upvotes: 1

Related Questions