I have some text in a database. I use French and English. French has accents, and some special characters like ç
. I use Mamp, MySQL and PHP.
I have collation latin1_swedish-ci
(the default). I tried utf8_general_ci
and the result is the same.
If I use in a html page, I have this in the head: <meta charset="UTF-8">
As an example, in the database I have "voilà".
When I echo the text from the database to html:
$con = mysqli_connect("localhost","root","root");
if (!$con) {
die('The connexion failed: ' . mysqli_error());
}
if (!mysqli_select_db($con, 'prova')){
echo "Connection with database was not possible";
}
$result = mysqli_query($con, "SELECT * FROM test1
WHERE id='1' ")
or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
$text = $row['first'];
echo $text; //I see: voil�
echo htmlentities($text); //I see nothing
echo utf8_encode($text); //This works: I see voilà
}
- Why
htmlentities
does not work? - Is
utf8_encode();
the way to go? I have to use that always when I output something from the database? Why do I have to use that if the collation is alreadyUTF8
? Is there any better way to store and output text with accents in a MySQL database?