raygo
raygo

Reputation: 1398

Charset is not working - Spanish characters

This is really driving me crazy. I'm building a spanish website (meaning a lot of latin characters) and I'm using Zend framework.

When I save a á it displays like á . I know is a charset encoding but I dont understand why.

My head charset is <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> My database is utf8_general_ci . I've changed the charset to others and always the same problem.

When I look in my database, what is saved is an á

Any idea why is this happening? Thanks!

Upvotes: 1

Views: 1885

Answers (2)

Carlos
Carlos

Reputation: 5072

Here goes the recipe to get rid of encoding headaches:

  1. Your DB, tables and fields must use collation utf8_unicode_ci.
  2. Be sure your code (HTML, PHP... all) is UTF-8 encoded.
  3. Always use this meta tag: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. This is long:

If you have access to MySQL file configuration my.cnf just add this line:

init-connect='SET NAMES utf8'

This tells MySQL to return results in UTF-8 for each connection.

If you cannot edit my.cnf but you are using PDO, open the connection this way:

$pdo = new PDO($dsn, $usr, $pwd, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

If you aren't using PDO... start using it, what are you waiting for? Meanwhile you can execute this after each connection if you use ext/MySQLi:

$mysql->set_charset('utf8');

Or this if you use plain old ext/MySQL:

mysql_set_charset('utf8', $connection);

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86346

The character á and other characters outside the ASCII map can be mapped with unicode.

As you are stroing the data in a table with UTF-8 charset and characters are storing properly.

I would suggest you that

  • Before you store data
  • Before you fetch data

Run the query SET NAMES UTF-8. This will fetch your UTF-8 data with taking care of the characters which are outside of ASCII map.

While outputting the data you make sure that you are forcing/directing browser to use the UTF-8 charset to print the data.

Upvotes: 0

Related Questions