Samdeesh
Samdeesh

Reputation: 945

Characters getting encoded to �

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

Upvotes: 0

Views: 48

Answers (2)

Chris G
Chris G

Reputation: 780

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

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

Upvotes: 0

Qirel
Qirel

Reputation: 26490

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.

Upvotes: 2

Related Questions