phpmysqlutf-8mamp

Weird UTF-8 MAMP issue


in further text, I'll refer to some characters as correct UTF characters, that's utf-8 encoded character in it's ok form (á) and incorrect UTF characters, that's the same, but messed up utf-8 character (e.g. showing as ý).

So, there's CMS running on MAMP – it's locally developed website for client. Now, in administration, I input correct utf-8 characters. When browsed on localhost, it shows up as correct utf-8 characters. In utf-8 encoded mysql, however, it shows as incorrectt utf-8 characters (in Sequel Pro/phpMyAdmin).

When the database was moved to staging/dev server, the very same DB that works on MAMP, stopped working and show up as incorrect – just as they are input on MAMP.

However, if I open administration (the very same code) on staging server and input correct characters, not only they show up normal, but they are also saved in MySQL as correct characters.

I'm actually shocked – the very same code, the very same db and it still behaves in a different way.

Have you ever noticed something like, i.e. can you spare me of the pain of looking through millions of different entry points, where utf-8 can be messed up.

P.S.: I think this might be MAMP-related (weird utf-8 settings), not strictly app-related.
P.S.: The app uses MySQLi, every file is UTF-8 encoded :/ The only thing that differs is that on mamp I use just host/user mysql connection, while on staging db socket is introduced.


Solution

  • I've actually experienced this a few times. I do the following to work around it.

    As soon as I connect to the database:

    mysql_query("SET NAMES 'utf8'", $db_con);
    

    At the top of each page:

    header('Content-Type: text/html; charset=utf-8');
    

    If you want to be really pedantic, with any forms you use to post data:

    <form accept-charset="utf-8"></form>