UTF-8 MySQL and Charset, pls help me understand this once and for all!

Posted by FFish on Stack Overflow See other posts from Stack Overflow or by FFish
Published on 2010-05-31T13:49:34Z Indexed on 2010/05/31 13:53 UTC
Read the original article Hit count: 273

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about mysql

Related posts about utf-8