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

4
  • which mysql extension is used? have you tried to set the character set to utf-8 for database communication? Commented May 3, 2012 at 15:38
  • As jeroen already answered, I had to include mysql_set_charset('utf8'); in the code :) But thanks for answering! :) Commented May 3, 2012 at 15:44
  • you should mark his answer then to close the question Commented May 3, 2012 at 16:03
  • Sorry, I had to "wait 8 more minutes to mark an answer" :) Commented May 3, 2012 at 16:10

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

5 Comments

as far as i know the php file itself doesn't matter in that case
@Hajo I have to admit I've never tried it with a non-utf8 file...
well, the php-file itself does only matter if you print some special chars directly in html/php-code.
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.
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!
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" />)

Comments

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.

3 Comments

But dont you get a str_replace function like 50 lines long or something?
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.
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 :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.