2

I am facing a paradox in decoding with utf8_encode decode. I have a MySQL database with uft8 collation and whose fields have the utf8_general coding. I have my php file in utf8, and in my HTML pages I have specified in the header the utf8 charset.

My problem is that when I select from my table a field containing accented characters (like èçò ùé) and echo that to the browser, I get strange characters.

To resolve my problem, I have to echo $description=utf8_encode($imm['description']).

My question is why can’t I do the echo directly without having to use uft8_encode every time?

tchrist
  • 78,834
  • 30
  • 123
  • 180
albanx
  • 6,193
  • 9
  • 67
  • 97

2 Answers2

2

I'll just guess that your database connection is not set to UTF-8.
See SET NAMES utf8 in MySQL?

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
1

you need to specify the header using php to be utf-8. also make sure that the format of the chars is utf-8 before storing in the db because utf_encode encodes an ISO-8859-1 string to UTF-8, which most likely means that the chars are being stored as ISO-8859-1 in s a utf-8 table.

make sure that you convert those chars in utf-8 before storing them in the db and then echo should not be a problem at all.

Source: had the exact same problem myself.

Vish
  • 4,508
  • 10
  • 42
  • 74