Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP charset accents issue

I have a form in my page for users to leave a comment.

I'm currently using this charset:

meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" 

but retrieveving the comment from DB accents are not displaying correct ( Ex. è =>è ).

Which parameters should i care about for a correct handling of accents?

SOLVED

  • changed meta tag to charset='utf-8'

  • changed character-set Mysql (ALTER TABLE comments CONVERT TO CHARACTER SET utf-8)

  • changed connection character-set both when inserting records and retrieving ($conn->query('SET NAMES utf8'))

Now accents are displaying correct

thanks

Luca

like image 371
luca Avatar asked Oct 12 '22 06:10

luca


1 Answers

Character sets can be complicated and pain to debug when it comes to LAMP web applications. At each of the stages that one piece of software talks to another there's scope for incorrect charset translation or incorrect storage of data.

The places you need to look out for are: - Between the browser and the web server (which you've listed already) - Between PHP and the MySQL server

The character you've listed look like normal a European character that will be included in the ISO-8859-1 charset.

Things to check for:

  • even though you're specifying the character set in a meta header have a look in your browser to be sure which character set the browser is actually using. If you've specified it the browser should use that charset to render/view the page but in cases I've seen it attempting to auto-detect the correct charset and failing. Most browsers will have an "encoding" menu (perhaps under "view") that allows you to choose the charset. Ensure that it says ISO-8859-1 (Western European).
  • MySQL can happily support character set conversion if required to but in most cases you want to have your tables and client connection set to use the same encoding. When configured this way MySQL won't attempt to do any encoding conversion and will just write the data you input byte for byte into the table. When read it'll come out the same way byte for byte.

You've not said if you're reading data from the database back out with the same web-app or with some other client. I'd suggest you try to read it out with the same web application and using the same meta charset header (again, check the browser is really setting it) and see what is displayed in the browser.

To debug these issues requires you to be really sure about whether the client/console you're using is doing any conversion too, the safest way is sometimes to get the data into a hex editor where you can be sure that nothing else is messing around with any translation.

If it doesn't look like it's a browser-side problem please can you include the output of the following commands against your database:

Run from a connection that your web-app makes (not from some other MySQL client):

SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';

Run from any MySQL client:

SHOW CREATE TABLE myTable;

(where myTable is the table you're reading/writing data from/to)

like image 89
James C Avatar answered Oct 15 '22 11:10

James C