Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Jonathan Söder
Jonathan Söder
5,771 Points

Not have to specify Charset in connection? PHP and MYSQL


I've got a minor problem that's really annoying me.
I've set database, table and my php file to UTF-8.
If I echo swedish characters they show up in the browser. Fine.
However if I retrieve swedish characters from the database they show up as "?".
AKA something's wrong with the decoding.
When I specify charset with mysqli_set_charset() in my connection snippet characters decode properly.

It's been a while since I coded but I'm pretty sure I never had to specify a charset in my connection before.
If the same charset is set everywhere, you shouldn't have to specify the charset in the connection, right? Does anyone have any hunch of what's going on here?

everything works fine with mysqli_set_charset.
Without function, the info fetched from db displays special chars as "?".
DB, Table & phpfile set to UTF-8.
simple php echo's displays special chars fine.
Why is function needed when everything's set to same charset?

1 Answer

Andrew Winkler
Andrew Winkler
37,739 Points

I've never run into such an error before, but I did understand what you were asking. I found the answer on a stack overflow thread.



After connecting and before making any queries.

Your database charset is just for storage, not for transmission between app and database.