1

Possible Duplicate:
Special characters in PHP / MySQL

I have a problem. I have a piece of text in my database (MySQL 5.5.20) with characters like 'é' and " ' " who aren't displaying properly after executing the MySQL query and displaying it with echo($...). With every special character I've inputted in the database, it displays a small question mark inside a diamond. If I look at the text in the database itself, it is an 'é' and " ' ", so I figured the problem isn't MySQL.

One thing I could do is str_replace everything like " ' " --> "'" on input, but then I have to do this for every character there is. Oh and I already have included

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

and this didn't work.

Hopefully you've all the information to help me, if not just say :) Thanks in advance!

Milaan

Community
  • 1
  • 1
Milaan
  • 247
  • 5
  • 14

3 Answers3

5

You need to have everything in utf-8:

  • The database field
  • The database connection (mysql_set_charset('utf8'); in classic mysql, something like $db->exec('SET CHARACTER SET utf8'); in PDO)
  • The content type (like you have already)
jeroen
  • 88,615
  • 21
  • 107
  • 128
  • 1
    as far as i know the php file itself doesn't matter in that case – Hajo May 03 '12 at 15:38
  • @Hajo I have to admit I've never tried it with a non-utf8 file... – jeroen May 03 '12 at 15:40
  • well, the php-file itself does only matter if you print some special chars directly in html/php-code. – shadowhorst May 03 '12 at 15:42
  • i'm using ascii files by 99% as its the simplest way in most vcs, can easily be forced and there is no problem so far with output in iso, unicode or whatever charset as special characters are encoded with entities. – Hajo May 03 '12 at 15:43
  • mysql_set_charset('utf8'); This worked indeed, many thanks! I've been searching for this for hours now, thought I already fixed it by putting it in the META tags. But I was wrong! Thank you very much! – Milaan May 03 '12 at 15:43
0

I was using the SQL query SET NAMES utf8 right after the connection to a DB is done successfully for over a years. But this is not neccessary when You have everything in the same encoding

  • source files encoding
  • table columns collations
  • web page encoding (both in PHP header('Content-Type: text/html; charset=utf-8'); and in <header> <meta name="Content-Type" value="text/html; charset=utf-8" />)
shadyyx
  • 15,095
  • 5
  • 48
  • 91
0

I usually format all the input text with str_replace an replace all uncommon symbols with their &#xxx; equivalent, this is actually useful to prevent injection and bad html rendering

i.e. if someone inputs html tags they'll be active in your page and so on.

Onheiron
  • 1,993
  • 1
  • 23
  • 36
  • But dont you get a str_replace function like 50 lines long or something? – Milaan May 03 '12 at 16:09
  • I do like this: output = str_replace('/regex expression/', input); output = str_replace('/next regex/',output); ... and so on till I replaced serially every character, I made a format(input) function to call on inputs. – Onheiron May 03 '12 at 19:44
  • That will also work I guess, but the answer above is much simpler and effective. It also doesn't take a lot more coding :) But thank you for your answer :) – Milaan May 04 '12 at 16:53