在MySQL中存储特殊字符(Android,PHP也涉及)

Ok, I am trying to store data in a MySQL database from an Android app. I use PHP as the go between.

What I submit to the database is this (for example): Sakè

What is stored in MySQL is: Sakè

When I print the line back out in Android it knows to convert back to: Sakè

The problem is, when I do certain select statements, yeah it basically doesn't work out like it should. Is there any way I can keep this consistent?

Just a note in MySQL: I use utf8_general_ci

You need to enforce correct character for your database connection for your PHP scripts. Simplest approach would be to execute this query:

SET NAMES 'utf8';

just once, best after you connect(). If you use Zend Framework, just add this:

resources.db.params.charset  = UTF8

to your application.ini. For mysqli see set_charset(). If you use anything else, check the docs for something similar. If there's nothing, the SET NAMES trick would always work.

One important note though - depending on how you put your data to the DB, your database content most likely require fixing as it is corrupted by broken charset during INSERTs. So if even you do SET NAMES now, you most likely still see old data shown wrong. But that is, ironically, OK - you are now showing all fine, but data itself is broken. You just now need to fix your data (just re-insert it).