0

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à
}
  1. Why htmlentities does not work?
  2. 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 already UTF8? Is there any better way to store and output text with accents in a MySQL database?
Nrc
  • 9,577
  • 17
  • 67
  • 114
  • Possible duplicate of [Getting special characters out of a MySQL database with PHP](http://stackoverflow.com/questions/15892610/getting-special-characters-out-of-a-mysql-database-with-php) – Gregor Menih Jan 23 '16 at 14:34
  • @ItsGreg. the other does not anwer my questions. I arrived to a similar solution, but the question is why – Nrc Jan 23 '16 at 14:43
  • `htmlentities()` should not return nothing if your string is `voilà`. What does the field look like in phpMyAdmin? – rybo111 Jan 23 '16 at 15:14
  • @rybo111: in phpMyAdmin I just have that: voilà. Just to test. Why it should return nothing? – Nrc Jan 23 '16 at 15:19
  • Can you try `mysqli_set_charset($con,"utf8");` before your query, where `$con` is your connection? – rybo111 Jan 23 '16 at 15:25
  • You forgot to @ me. What does `get_charset($con)` return? – rybo111 Jan 23 '16 at 18:16
  • Also can you include your mysqli connection in your code? – rybo111 Jan 23 '16 at 18:29
  • @rybo111: yes, it works. But as I said: echo utf8_encode($text); it works too. The question is why? Why do I need that, if already the database collation is utf-8? why htmlentities does not work? – Nrc Jan 24 '16 at 09:46
  • It's not an html entity – Strawberry Jan 24 '16 at 10:39
  • Not sure why you deleted your old comment and repeated it... What does `get_charset($con)` return? – rybo111 Jan 24 '16 at 10:49

1 Answers1

0

After you connect to the DB you should set the client charset to UTF8:

mysqli_set_charset($con, "UTF8");

Otherwise the mysql client transforms the UTF8 'voilà' to latin1 ('cause it seems that is it's default).

Either you tell the client that I want everything in UTF8, or you get it with the default latin1, and convert it one-by-one yourself calling utf8_encose($text)

Gavriel
  • 18,880
  • 12
  • 68
  • 105