0

i'm running a german website which gets content from a mysql database. i've defined the charset as utf8 as following:

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

the problem is, when fetching + displaying contents from the database i always need to use utf8_encode in order to get the proper german "umlauts".

i want to maintain the utf8 charset for my web as i'll have to add more languages which have special characters.

any ideas on how to 1:1 echo database contents without having to utf8_encode?

thanks

1

4 Answers 4

2

Hard to tell without seeing how you are connecting to your database, but a common problem is the database connection itself.

After opening / selecting the database you need to set:

$db->exec('SET CHARACTER SET utf8');    // PDO

mysql_set_charset('utf8');    // Deprecated mysql_* extension
Sign up to request clarification or add additional context in comments.

Comments

1

Whenever I want to use utf-8 with PHP and MySQL, I found that usually these two functions are the ones you should use after mysql_connect():

mysql_set_charset('utf8', $link);
mysql_query('SET NAMES utf8', $link);

Comments

0

Setting the content type in the header may do the trick:

header('content-type: text/html; charset=utf-8');

Comments

0

I had a similar problem and i solve adding this in the beginning of my PHP file:

ini_set('default_charset', 'UTF-8');
mb_internal_encoding('UTF-8');

Additionally, is very important to check if you are saving your PHP file in UTF-8 format without BOM, i had a big headache with this. I recomend Notepad++, it shows the current file encoding and allow you to convert to UTF-8 without BOM if necessary.

If you would like to see my problem and solution, it is here.

Hope it can help you!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.