views:

129

answers:

6

Can someone explain me when I set everything to UTF-8 I keep getting those damn ���

MySQL
Server version: 5.1.44
MySQL charset: UTF-8 Unicode (utf8)

I create a new database

name: utf8test
collation: utf8_general_ci
MySQL connection collation: utf8_general_ci

My SQL looks like this:

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

CREATE TABLE IF NOT EXISTS `test_table` (
    `test_id` int(11) NOT NULL,
    `test_text` text NOT NULL,
    PRIMARY KEY (`test_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `test_table` (`test_id`, `test_text`) VALUES
(1, 'hééélo'),
(2, 'wööörld');

My PHP / HTML:

<?php
$db_conn = mysql_connect("localhost", "root", "") or die("Can't connect to db");
mysql_select_db("utf8test", $db_conn)  or die("Can't select db");

// $result = mysql_query("set names 'utf8'"); // this works... why??
$query = "SELECT * FROM test_table";        
$result = mysql_query($query);

$output = "";
while($row = mysql_fetch_assoc($result)) {
    $output .= "id: " . $row['test_id'] . " - text: " . $row['test_text'] . "<br />";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html lang="it" xmlns="http://www.w3.org/1999/xhtml" xml:lang="it">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UTF-8 test</title>
</head>
<body>
<?php echo $output; ?>
</body>
</html>
+1  A: 

Try to set charachter encoding after mysql_connect function like this:

 mysql_query ("set character_set_client='utf8'"); 
 mysql_query ("set character_set_results='utf8'"); 

 mysql_query ("set collation_connection='utf8_general_ci'");
antyrat
can be shortened to simple `SET NAMES utf8`
Col. Shrapnel
Yes, I know. I write mysql 4+ resolving solution.
antyrat
4.0 understand none of these settings. And 4.1 understand set names as well
Col. Shrapnel
A: 

I didn't see a "SET NAMES 'utf8';" query just after connecting to your database.

Try it, may work for you.

zaf
I really love that "try it". children share their knowledge on magic things.
Col. Shrapnel
A: 

Hi,

take a look at the mysql_set_charset function. Perhaps you need to call it before you retreive the data.

becquerel
A: 

You want to check the current charset using mysql_client_encoding and when needed mysql_set_charset. Or just never mind the checking and blindly go with setting.

nikc
A: 

I set everything to UTF-8

Not quite.
You have to tell mysql your client's encoding.

As a matter of fact, you don't have to set up "everything" in utf-8. You can have your tables in latin1 and output in utf-8. Or contrary.
Very flexible.

But you have to set up client's encoding explicitly. So, that's why it works with set names utf8. Because this query setting up client's encoding. And let Mysql know that data must be sent in utf-8. Pretty sensible, huh?

Also I have to mention your SQL dump. It needs same setting. Just SET NAMES somewhere at the top. Because you are sending these queries from some client too. And this client's encoding needs to be set up as well.

And one more thing to mention: be sure your server sending proper encoding in the Content-type header. You didn't set it to UTF-8 too.

Col. Shrapnel
Thanks Col. Ok so let me see.. the SQL dump you are talking about.. Apart from the line $result = mysql_query("set names 'utf8'") in my example, I have to do another set names utf8? Where and how exactly please?
FFish
@FFish the same place where you do input your sql commands started from `SET SQL_MODE`. is it mysql console or what? it's just a simple rule: every time you insert or select data - client encoding must be set.
Col. Shrapnel
ok, the SQL dump, now I see what you mean. Cheers
FFish
A: 

I would say that you forget to set the content type encoding of your PHP file to utf-8:

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

Or is the encoding error within the MySQL database?

If only loading the data returns the wrong results, you can use the queries mentioned before or this line of code to enable UTF-8 for queries:

$mysqli->set_charset('utf8');

I hope that is what you needed.

Kau-Boy