views:

221

answers:

4

I am saving some foreign characters quite fine into my mysql database. This is the ones I am using right now for example: いち

When I try to echo out that column from the database, it only shows up as '??'

Why are there question marks showing up when I am trying to echo foreign characters?

A: 

What encoding are you using in your database, and what encoding are you sending to the browser either via header() or HTML? Make sure they match the data you're putting in.

Amber
A: 

You should change your database encoding as well as specify the encoding in the PHP headers to UTF-8. MySQL allows you to choose a default encoding, or on a per-table or per-field basis.

header('Content-Type: text/html; charset=UTF-8');

If you keep the encoding of your database and that of your scripts consistent you shouldn't have any problems.

Lotus Notes
+2  A: 

Most likely you didn't set proper Mysql client's encoding.
You must issue a SET NAMES <encoding> query where <encoding> is your HTML page encoding.

Please note that there are no "foreign characters" but only encoding.
You have to know what encoding your characters in and set it up properly in all three web-application parts:

  • Database. Table's encoding must be set to match actual data encoding.
  • PHP as a database's client encoding. SET NAMES mentioned above.
  • Browser's encoding. Set by header mentioned fellow SO'ers above or by other server setting.
Col. Shrapnel
A: 

In order for those characters to be supported, you must choose

  • a character set that contains those characters, typically Unicode
  • a character encoding that maps the characters to the underlying bytestream, typically, UTF-8 or, less frequently, UTF-16.

Let's go with Unicode/UTF-8, because that's the recommended standard. You must ensure these things:

  • The data you insert into the database is in UTF-8, or, if it isn't, you must convert it (e.g. with iconv or mb_convert_encoding). If you are insert data from HTML forms, make sure you add the attribute accept-charset="utf-8" to your <form> elements.
  • Your database must support unicode. In MySQL, see the CREATE DATABASE syntax and the supported character sets/collations (read: encodings). You should pick UTF-8/Unicode.
  • The database must know the PHP MySQL driver will be passed strings in UTF-8 (i.e., the client encoding must be set to UTF-8). In MySQL, this is done with executing the SQL statement SET NAMES UTF8.
  • The web server must declare its content is encoded in UTF-8. In PHP, this is done with header('Content-type: text/html; charset=utf-8');
Artefacto
check your code before post
Col. Shrapnel
@Col. Shrapnel pardon?
Artefacto
@Col. Shrapnel Fixed set names and accept-charset. BTW, feel free to fix it yourself.
Artefacto