为什么我的MySQL数据库中没有正确存储突出的字符?

I have a drupal form which saves user input into a MySQL database using the Doctrine library. The problem is that if the user input has accentuated characters, they don't look good in the DB. For example, Sempé becomes Sempé. This is strange to me because:

  • the field it is getting stored into is utf8: varchar(20) COLLATE utf8_swedish_ci NOT NULL,
  • the table is also utf8: ENGINE=InnoDB AUTO_INCREMENT=202 DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci
  • all my php scripts, including the one which tells drupal what fields to add into the form) are saved as utf-8
  • the form drupal creates accepts utf8: <form enctype="multipart/form-data" action="/drupal/identification" method="post" id="form-identification" accept-charset="UTF-8">
  • the page drupal creates is sent as utf8: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  • the php->mysql connection is set to utf8:

    $dbParams = array("driver" => "pdo_mysql",
        "host" => variable_get("dbManip_host"),
        "user" => variable_get("dbManip_user"),
        "password" => variable_get("dbManip_password"),
        "dbname" => variable_get("dbManip_dbName"),
        "charset" => "utf8");
    

I also tried:

  • adding default_charset = "utf8" to php.ini
  • passing the form data in utf8_encode before saving it
  • passing the form data in mb_convert_encoding before saving it

but none of those worked.

Am I missing anything here?

UPDATE

I had a look at the headers sent by the browser to the server and found out that $_SERVER['HTTP_ACCEPT_CHARSET'] === null

Prior to submit of any other query via a given PHP MySQL link, try:

mysql_query('SET NAMES UTF8');

This statement indicates that further queries submit via the same MySQL link will be in UTF8 charset.

When you want to later retrieve the MySQL data and publish to a web page via PHP, be sure to set the charset in the HTTP header:

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

...and convert to HTML entities in displayed content:

echo mb_convert_encoding($mysql_col_str, 'HTML-ENTITIES', 'UTF-8');

(this final statement assumes a variable $mysql_col_str has been defined which contains the UTF8 value returned by the MySQL query)

You should try adding an extra parameter called charset to the configuration of the Entity Manager:

$conn = array(
  'driver' => 'pdo_mysql',
  'host' => 'hostname',
  'user' => 'user',
  'password' => 'pass',
  'dbname' => 'dbname',
  'charset' => 'utf8'
);

$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);