Ales
Ales

Reputation: 537

MySQL: ucs2_slovenian_ci to utf-8 on webpage

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.

Upvotes: 4

Views: 301

Answers (2)

hakre
hakre

Reputation: 197775

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.

Upvotes: 2

dr.dimitru
dr.dimitru

Reputation: 2702

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");

Upvotes: 1

Related Questions