4

I have a MySQL database with tables in ucs2_slovenian_ci encoding. I would like to write content of tables on utf-8 encoding webpage. So far I tried with:

mysql_set_charset ("utf-8");
mysql_query("SET NAMES 'utf-8'");
...
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
...
<?php echo utf8_encode($text); ?>
...

I'm still geting weird signs (question marks) instead of letters Š,Č,Ž Nothing seems to work.

Weird thing is that with php command mysql_client_encoding($link) it says I have latin1 encoding. When I look page in Firefox it says UTF-8. What's wrong?

Please help.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Ales
  • 527
  • 2
  • 8
  • 24
  • `ucs2_slovenian_ci` is a database collation, while encoding is `ucs2`. You need something like `iconv("ucs-2", "utf-8", $text)`, not `utf8_encode` (which is `iconv("utf-8", "iso-latin-1", $text)`) – kirilloid Apr 15 '12 at 21:50

2 Answers2

2

ucs2_slovenian_ci encoding

That is not an encoding but a collation. A collation is the information on how something is sorted.

For your website, this isn't even of interest, because that's what the database knows about itself - or better: the data stored in itself.

For your website script's it's more important that you tell your database which encoding you need - here: UTF-8.

You signal that the database server by specifying the database client encoding. Consult your database client driver manual how to specify that. It could be that:

mysql_set_charset("utf8", $link);

Take care: it's utf8 while officially it's written UTF-8. That's something special with the MySQL database, write UTF-8 as utf8 when you set that parameter.

Do not use:

mysql_query("SET NAMES 'utf-8'");

because it's deprecated (not good, see as well the PHP manual and Whether to use “SET NAMES”).

And if you tell the database client which encoding you expect, you don't need to encode your own, like this:

utf8_encode($text);

Remove that stuff, you don't need it.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
1

First add header('Content-Type: text/html; charset=utf-8');

and

mysql_set_charset ("utf-8");

should be:

$db = mysql_connect($hostname,$username,$password);

mysql_set_charset ("utf8", $db);

Also make sure what in MySQL DB everything in utf-8

Anyways you may play with:

mb_convert_encoding($string, "Other encoding", "UTF-8");

Community
  • 1
  • 1
dr.dimitru
  • 2,645
  • 1
  • 27
  • 36
  • mb_convert_encoding just ignores special charecters instead of writting them I've already try with mysql_set_charset ("utf8"); and it doesn't work so doesn't header... It's not problem with page itself, it's problem with data that are in database in ucs-2. Cannot change encoding in DB because I don't own it – Ales Apr 15 '12 at 22:12
  • @user1077442: You don't need to change encoding in database, because mysql does the re-encoding on the fly for you if you tell the mysql database which encoding you need, like `utf8`. It will give you `utf8`. If you break it later on your own, that's your fault. But mysql does what it was commanded to do. – hakre Apr 15 '12 at 22:14
  • Ok, now I somehow got it working. It's not perfect but now at least i can replace characters with right ones. I also noticed that htmlentities was big problem for not showing like it suposed to. Thank you all – Ales Apr 15 '12 at 22:25