Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question mark characters on HTML if I use UTF-8 and weird characters on SQL data if I use ISO-8859-1 [duplicate]

I'm making a page with latin accents like á, ã, ç, and others. This site pulls data from a SQL database. I'm using this on the <head>:
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"/>
With this header the HTML accented characters are fine, but the SQL data is being displayed as çãinstead of ão, and if I change the charset from ISO-8859-1 to UTF-8, all the HTML accented characters are displayed by a � (question mark) and the SQL data shows accents just fine.
Is there any way to fix it besides escaping either all the HTML characters or the SQL ones?

PS: I've already tried mysql_set_charset('utf8'); and SET NAMES utf8, neither worked to me.

like image 347
Gus Avatar asked Jul 26 '13 06:07

Gus


People also ask

Why does a diamond with a question mark in it appear in my HTML?

If you get those diamonds then you either have a wrong encoding set or there is a problem with the (Verdana) font that is used. Western (ISO 8859-1) is the default encoding in Firefox.

What does UTF-8 do in HTML?

The HTML5 Standard: Unicode UTF-8 Unicode enables processing, storage, and transport of text independent of platform and language. The default character encoding in HTML-5 is UTF-8.

What is ISO 8859 character set?

Latin-1, also called ISO-8859-1, is an 8-bit character set endorsed by the International Organization for Standardization (ISO) and represents the alphabets of Western European languages.

Is UTF-8 a character?

UTF-8 is a character encoding system. It lets you represent characters as ASCII text, while still allowing for international characters, such as Chinese characters. As of the mid 2020s, UTF-8 is one of the most popular encoding systems.


1 Answers

When you see question marks, your document has not been stored in the correct encoding (should be UTF-8 in your case) or it isn't being served with the correct headers and/or meta tags.

If you want to work with special characters like è, your html document should be saved as UTF-8 and served as UTF-8:

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

Additionally, you have to use UTF-8 for your database connections:

…("SET NAMES utf8");
…("SET CHARACTER SET utf8");

And last but not least, you have to use UTF-8 for the database itself.

As you'll notice, you're already on the correct path… you just have to "use it all" (as I described above) instead of trying one thing at a time and ignoring the rest. It's the combination that makes it work. Simpler said: if you go "UTF-8", you will have to think "UTF-8" everywhere and stick to it in your html files, your headers, your meta tags, your database connections, and the database(s). Use the same encoding everywhere and stick to it, instead of using "a bit UTF-8 here and a bit ISO-whatever there".

like image 199
e-sushi Avatar answered Sep 21 '22 18:09

e-sushi