David Chouinard
David Chouinard

Reputation: 6836

Accented characters stored in MySQL database

I have a webapp that stores French text -- which potentially includes accented characters -- in a MySQL database. When data is retrieved directly through PHP, accented characters become gibbirish. For instance: qui r�fl�te la liste.

Hence, I use htmlentities() (or htmlspecialchars() ) to convert the string to html entities, and all is fine. However, when I come to output data that contains both accented characters and HTML elements, things get more complicated. For instance, <strong> is converted to &lt;strong&gt; and therefore not understood by the browser.

How can I simultaneously get accented characters displayed correctly and my HTML parsed correctly?

Thank you!

Upvotes: 2

Views: 8093

Answers (3)

Veger
Veger

Reputation: 37905

You could set the Collation of the database fields containing the accented character to utf8_general_ci to support them.

Eventually you can set the collation of the database as well, so all fields are set by default.

Upvotes: 0

yhager
yhager

Reputation: 1662

You should use UTF-8 encoding for storing the data in the database - then everything should work as expected and no htmlentities() will be required.

Make sure all aspect are utf-8 - the database, the tables encoding and collation, and the connection, both on the client and server side. Things might work even if not everything is utf-8, but might fail horribly when you will do backup & restore - that is why I recommend utf-8 across the board.

Upvotes: 4

OcuS
OcuS

Reputation: 5310

Maybe you could take a look to utf8_encode() and utf8_decode()

Upvotes: 10

Related Questions