0

I have a word that have "ñ/Ñ" characters in my mysql. But when I printed it replaced by a diamond with a question mark sign. This is what it did so far but didn't work.

<meta charset="utf-8">
mysql:host=localhost;dbname=dbnameko;charset=UTF-8
Collation : latin_sedish to utf8_general_ci

I google it and searched stackoverflow threads already but no answer was right.

arjay0601
  • 475
  • 2
  • 8
  • 16
  • You should also make the connection to your database use UTF-8 (`SET NAMES 'utf8'`) – F.P Nov 29 '12 at 12:45
  • or, if you haven't the rights to change the database, before you call mysql_query([YOUR QUERY]), call mysql_query("set names utf-8") – Valentino Ru Nov 29 '12 at 12:47
  • Was it an Apache or a MySQL connection problem after all? Was any of the answers helpful or was it a different problem? – Miltos Kokkonidis Dec 01 '12 at 00:59
  • @MiltiadisKokkonidis This was the answer: mysql_query("set names utf-8") – arjay0601 Dec 01 '12 at 03:54
  • MySQL connection then. Don't you just hate it when default settings conspire against honest programmers trying to write a proper application that just works with something as rudimentary as strings, especially given that we have Unicode since 1991 ... Anyway, I have provided a link to an answer about setting the default character set correctly in my answer in case that helps. – Miltos Kokkonidis Dec 01 '12 at 08:44

2 Answers2

0

We have Unicode since 1991 so it is rather regrettable that such problems still keep us from doing our work. Unfortunately, the defaults are sometimes all wrong for i18n so we have to do things yourselves ...

You have done the right thing as far as HTTP is concerned, but you have to do the right thing for the MySQL connection ...

Try this:

  $result = mysql_query("SET NAMES utf8; SET CHARACTER SET utf8;");
  $cmd = "select * from yourtable";
  $result = mysql_query($cmd); //all should data should arrive correctly encoded

To avoid having to do that in each connection you can change the .cnf file. Please have a look at the accepted answer to the following SO question: Change MySQL default character set to UTF-8 in my.cnf?


UPDATE: It turns out that the problem was indeed with the SQL connection string. However, on Apache AddDefaultCharset can also cause problems, so I leave this part of the answer.

AddDefaultCharset is nasty if you don't understand what it does! If it is set, Apache will set the charset for responses of text/html and text/plain content types. This effectively means that your will be ignored. Either set it to utf-8 as Sacx's recomments, or to off. (Incidentally if it is set not to a specific charset but to "on", this will be the same as if it was set to "iso-8859-1", which makes debugging such situations so much harder!) More info at: http://httpd.apache.org/docs/2.2/mod/core.html


See also: How to make MySQL handle UTF-8 properly

Community
  • 1
  • 1
Miltos Kokkonidis
  • 3,888
  • 20
  • 14
0

Your web server doesn't know about this character set. If you use Apache add this line in your configuration file or .htaccess

AddDefaultCharset utf-8
Sacx
  • 6,276
  • 4
  • 22
  • 29