Getting Encoding Issue Even when Header to Set UTF-8 is Used

132 views Asked by At

I have a PHP script:

<?php
header('Content-Type: text/plain; charset=utf-8');//i have already used the 
  //header for encoding to utf-8

//rest of the code.
?>

I have already used the header for encoding to UTF-8, but when the values are fetched from the MySQL database and echoed, they are not displaying the proper output as in my MySQL database.

Detailed Explanation:

I have one print screen where the data is displaying correctly.

But when I export the same data in PDF, the encoding issue is occurring.

So I don't think this is a database issue.

Also the PHP code responsible for exporting the document to PDF contains the above mentioned header to set UTF-8.

Example:

"Meeùs" is displayed as "Mees".

Note:

My database is already set to UTF-8.

What I am doing wrong?

2

There are 2 answers

0
keegzer On

Because your database is not UTF8.

Try to set your db encoding to UTF-8, or use utf8_encode($data) into your code after extract from database.

Check if your IDE encodes your file to utf-8 without BOM.

0
peterh On

This symptom clearly indicates an encoding problem between the database and the php, and not between the php and the browser.

MySQL needs a

SET NAMES 'utf8';

on each connection.

It is better if also your tables are in utf8, although mysql has the needed on-the-fly conversion capability, if they aren't.