Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Databases

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

Not have to specify Charset in connection? PHP and MYSQL

Hi!

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?

So:
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,737 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.

Call

mysql_set_charset("utf8");

After connecting and before making any queries.

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