[web] Mysql: client not understanding the set up encoding

Started by
6 comments, last by PERECil 18 years ago
Hi, We're currently in the process of changing one of our web application to be fully in utf-8 encoding standard. However, I got a little problem: our php-mysql connector doesn't seems to take account the new encoding set up with SET NAMES. Here is an example:

<?php
function showVars($s)
{
  $r = mysql_query("SHOW VARIABLES LIKE '%char%'",$s);
  while($pResult=mysql_fetch_assoc($r))
	echo "variable name: ".$pResult["Variable_name"]." value: ".$pResult["Value"]."";
}

$s = mysql_connect("localhost","root","");
showVars($s);
echo mysql_client_encoding($s)."";
mysql_query("SET NAMES 'UTF8'",$s);
showVars($s);
echo mysql_client_encoding($s)."";
?>
The answer:

variable name: character_set_client value: latin1
variable name: character_set_connection value: latin1
variable name: character_set_database value: utf8
variable name: character_set_results value: latin1
variable name: character_set_server value: utf8
variable name: character_set_system value: utf8
variable name: character_sets_dir value: /usr/share/mysql/charsets/
latin1
variable name: character_set_client value: utf8
variable name: character_set_connection value: utf8
variable name: character_set_database value: utf8
variable name: character_set_results value: utf8
variable name: character_set_server value: utf8
variable name: character_set_system value: utf8
variable name: character_sets_dir value: /usr/share/mysql/charsets/
latin1
You can see that the character_set has been put to utf-8 with the "SET NAMES" command, but the client stays in latin1. Does someone have an idea about this problem? System configuration: the computer is configured with latin1 the server is fully utf-8 and run MySQL 4.1.15-Debian_1-log
Advertisement
Maybe you need SET CHARACTER SET instead, or as well.

Why append ."" to all your output statements, by the way?
I don't see what's wrong with that.

"SET NAMES utf8" must be done on a per-connection basis. Send it before you do any other SQL for each connection and you will be fine.

I've used it in several application and utf8 strings have been going into my datbase fine.

One thing I should warn you about though ... you still need to have the tables character sets in utf8 if you want to store unicode data. This can be done either in the CREATE TABLE statement, or you can set the database default charset before you start, then all your char columns will be utf8 unless you specify otherwise.

Mark
The ."" is by the way a ."
";

I've tried with set character set too. And I know that the "SET NAMES" should be done everytime I create a new connection, but if you read the code carefully, I only made one connection.

Oh, and all my databases/tables are in utf8_general_ci.
Well it all looks fine to me.

What seems to be the problem?

Mark
mysql_client_encoding return latin1, but show variables tells that the client should be in UTF-8.
In which case, it is apparently wrong.

The mysql_ functions are the "old" way to access MySQL anyway- new applications should use mysqli or PDO::MySQL (Or some data layer which uses them)

mark
Ok, i'll test with mysqli functions... i'll keep you informed.

This topic is closed to new replies.

Advertisement