Question mark characters in HTML if I use UTF-8 and weird characters in SQL data if I use ISO-8859-1

I make a page with Latin accents like á, ã, ç and others. This site retrieves data from an SQL database. I use this on <head> :
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"/>
With this header, HTML accented characters are good, but SQL data is displayed as çã instead of ão , and if I change the charset from ISO-8859-1 to UTF-8, all HTML accented characters are displayed with a character (question mark), and SQL data shows accents just fine.
Is there a way to fix this other than escaping all HTML characters or SQL files?

PS: I already tried mysql_set_charset('utf8'); and SET NAMES utf8 , did not work with me.

+6
source share
1 answer

When you see <question marks , your document was not saved in the correct encoding (it should be UTF-8 in your case), or it is not served with the correct headings and / or meta tags.

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

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

In addition, you must use UTF-8 to connect to the database:

... ("SET TITLES utf8");
... ("SET CHARACTER SET utf8");

Last but not least, you must use UTF-8 for the database itself.

As you will notice, you are already on the right track ... you just need to “use it all” (as I described above), instead of trying to ignore the others at the same time. This is 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, headers, meta tags, connections to the database and the database. Use the same encoding everywhere and stick to it, instead of using the "UTF-8 bit here and the ISO bit there."

+6
source

Source: https://habr.com/ru/post/950328/


All Articles