0

I have a registration form which corresponds correctly to db table if the language is English. IF language is Greek or sth else the username appears in table as encrypted (corrupted??). In the db table the utf8_general_ci is selected. What should i add in my code in order to have the same result in all languages. (result= in the db table see correctly user's username).

3 Answers3

1

Make sure the page that is serving the result is of the same encoding as the database's encoding. If you have UTF-8 data in your tables, but the page displaying these data are for instance ISO-8859-1, characters outside the ISO-8859-1 character set range would show up as strange.

In PHP one can modify headers for this:

header('Content-type: text/html; charset=utf-8');
Coreus
  • 5,360
  • 3
  • 35
  • 50
0

Before your first query add

mysql_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
Amir
  • 4,089
  • 4
  • 16
  • 28
  • 1
    Better use mysql_set_charset() instead of mysql_query(). http://www.php.net/manual/en/mysqlinfo.concepts.charset.php. Or even better, use PDO: http://stackoverflow.com/questions/584676/how-to-make-pdo-run-set-names-utf8-each-time-i-connect-in-zendframework – Chuan Ma Mar 12 '13 at 03:16
0

UTF8 is very tricky in php. A few more issues (besides the utf8 header and utf8 db connections in the other 2 answers) I know of:

  1. You will need to use mbstring functions. For example, substr() may need to be replaced by mb_substr() function. For a complete list of string functions. see here. To let php know your expected internal encoding, call this function at the beginning of your script:

    mb_internal_encoding("UTF-8");

  2. Depending on your php version, you may have to handle htmlspecialchars(), htmlentities() differently.

    Like htmlspecialchars(), htmlentities() takes an optional third argument encoding which defines encoding used in conversion. If omitted, the default value for this argument is ISO-8859-1 in versions of PHP prior to 5.4.0, and UTF-8 from PHP 5.4.0 onwards.

  3. Old queries using mysql LENGTH() function may not be what we want anymore, because length() returns byte length, not character length. We MAY need to update them to use CHAR_LENGTH(). However, it all depends on the context! In the following example, the Chinese column has 3 characters, but each one take 3 bytes.

mysql> select *, length(d), char_length(d)  from t where id=3;
+----+-----------+-----------+----------------+
| id | d         | length(d) | char_length(d) |
+----+-----------+-----------+----------------+
|  3 | 被冻死 |         9 |              3 |
+----+-----------+-----------+----------------+
Chuan Ma
  • 9,754
  • 2
  • 45
  • 37