0

I am using php + mysql to make a dynamic page. My db has “Make which is encoded to �Make in the web page. I though it to be an encoding issue so,I tried using <html lang='en' dir='ltr'> & <meta charset="utf-8" /> But that too didn't help

Samdeesh
  • 905
  • 11
  • 23
  • I even tried `header('Content-type: text/plain; charset=utf-8');` & `header('Content-type: text/html; charset=utf-8');` but still the same issue – Samdeesh Dec 26 '15 at 13:07
  • I'm quite sure it is an encoding problem, but you should have more information in order to fix it. Are the characters OK in mysql? What is the correct character? What is the encoding used there? What exactly is written to HTML (� just means it can't be displayed it because it is invalid, but it does not show why it is invalid)? – zvone Dec 26 '15 at 13:09

2 Answers2

2

When dealing with any charset, it's important that you set everything to the same. You mentioned having set both PHP and HTML headers to UTF-8, which often does the trick, but it's also important that the database-connection, the actual database and it's tables are encoded with UTF-8 as well.

Connection

You also need to specify the charset in the connection itself.

  • PDO (specified in the object itself):
    $handler = new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'username', 'password', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET UTF8"));
  • MySQLi: (placed directly after creating the connection)
    • For OOP: $mysqli->set_charset("utf8");
    • For procedural: mysqli_set_charset($mysqli, "utf8");
      (where $mysqli is the MySQLi connection)
  • MySQL (depricated, you should convert to PDO or MySQLi): (placed directly after creating the connection)
    mysql_set_charset("utf8");

Database and tables

Your database and all its tables has to be set to UTF-8. Note that charset is not exactly the same as collation (see this post).

You can do that by running the queries below once for each database and tables (for example in phpMyAdmin)

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

File-encoding

It might also be needed for the file itself to be UTF-8 encoded. If you're using Notepad++ to write your code, this can be done in the "Format" drop-down on the taskbar (you should use Convert to..., as this won't mess your current file up) - but any decent IDE would have a similar option. You should use UTF-8 w/o BOM (see this StackOverflow question).

Other

  • It may be that you already have values in your database that are not encoded with UTF-8. Updating them manually could be a pain and could consume a lot of time. Should this be the case, you could use something like ForceUTF8 and loop through your databases, updating the fields with that function.

Should you follow all of the pointers above, chances are your problem will be solved. If not, you can take a look at this StackOverflow post: UTF-8 all the way through.

Community
  • 1
  • 1
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • `PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET UTF8"` solved the problem rest of the things were already in place – Samdeesh Dec 30 '15 at 17:11
0

If the � is in your database column itself, change the original character to the following:

http://www.w3schools.com/charsets/ref_html_ansi.asp

Chris G
  • 787
  • 6
  • 20