Shawn
Shawn

Reputation: 11391

Why aren't accentuated characters stored properly in my MySQL database?

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:

I also tried:

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

Upvotes: 0

Views: 228

Answers (2)

Nathan
Nathan

Reputation: 1700

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)

Upvotes: 1

PachinSV
PachinSV

Reputation: 3780

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);

Upvotes: 1

Related Questions