When I retrieve values from MySQL table to my PHP Zend Framework-application, the characters ä and ö are returned from the database to my browser like this �.
Any ideas where the problem might occur? I have tried setting some properties to UTF-8, but still this occurs...I think I have still missed something.
Could you give some solutions how I can get rid of this for sure? What configs I need to set etc.
preguntado el 31 de julio de 12 a las 15:07
When characters of the wrong encoding are read to the browser, they come up as � or ?. There is an encoding mismatch somewhere:
- en tu navegador
- In the database table collation
- In the HTML charset
Those characters would work in Unicode UTF-8, so you should verify that your DB table collation is
utf8_bin or similar, as, if you want to store everything in UTF-8 Swedish, you might use
In your browser, ensure that your content encoding is set to auto-detect.
To create a UTF-8 / utf8_bin table in MySQL, here is an example:
CREATE TABLE `sample` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
To ALTER an existing table to use UTF-8, use the following command:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;
Esta entrada has a good explanation of whether or not to use
CONVERT TO CHRACTER SET utf8
COLLATE must both be set, otherwise you will automatically get
For HTML, charset can be set using:
I woudl suggest you have a look into your "website encoding" set in the browser.
If the website does not supply a encoding and your mysql/php is handling it "correctly" this could happen (e.g. its set to "western 1252" but requires utf8/unicode)
In Firefox you can see the encoding via view -> encoding (or similar)
Con respecto a Zend_Db_Adapter_Pdo_Mysql make sure that your constructor is using UTF-8, Así:
$your_pdo = Zend_Db::factory('Pdo_Mysql', array( 'host' => DATABASE_HOST, 'username' => DATABASE_USERNAME, 'password' => DATABASE_PASSWORD, 'dbname' => DATABASE_DBNAME, 'options' => array( 'charset' => 'utf8' ) ) );
And before using data in HTML escape it:
return htmlspecialchars( $string, ENT_QUOTES, 'UTF-8' );
Check out all other answers for Content-Type, file encoding and more.
You just have to properly set your encoding to UTF-8. Here are the basic steps for a minimal working solution :
- Asegúrate de que tu DB connection uses UTF-8
- Make sure your HTTP content-type is set to UTF-8 (Ex:
header('Content-type: text/html; charset=utf-8');(also, you may specify the encoding for your document)
- Save your file as UTF-8. In Windows, files are saved as CP-1251, which means that special accented chars are not treated as UTF-8 encoded bytes (thus throw off what is being output to the browser. You have to explicitly set your encoding when saving your files. In Linux/Mac, this is not a problem as UTF-8 is used by default.
- Asegúrate de que tu
php.inidoes not override the encoding. If you have foreign
.htaccessfiles, check that they do not so either.
If you correctly handle UTF-8, you won't require
htmlentities etc. and will be able to properly output accented characters without special treatement.
Nota: : some locales are not handled by UTF-8, notably Tailandés.