Re: Encoding problem...
Posted by:
Ted Ashton
Date: August 08, 2010 04:01PM
Hi Rick,
Thanks again for your response. Unfortunately I do not have access to /etc/my.cnf on either the staging of final server. I presume the command you suggested (init_connect = 'SET NAMES utf8' ) needs to go under the [mysqld] section. In any case, when I put it under the [client] section on my local server starting the mysql client gave an error - something like invalid syntax, I presume because the server controls the connection encoding? I also presume placing this line in ~/.my.cnf wouldn't work?
On your suggestion I amended my test php script to use the following query:
…
$query = sprintf("
SELECT page_id,
page_title,
HEX(page_title) as hex,
LENGTH(page_title) as len,
CHAR_LENGTH(page_title) as charlen
FROM pages WHERE page_id = 399 OR page_id = 261;");
…
while ($row = mysql_fetch_assoc($result)) {
echo "\n<br />";
echo "\n<br />page_id: " . $row['page_id'] . ", " .
"\n<br />page_title: " . $row['page_title'] . ", " .
"\n<br />hex: " . $row['hex'] . ", " .
"\n<br />length: " . $row['len'] . ", " .
"\n<br />char length: " . $row['charlen'];
}
…
Note: the above code does NOT contain the line mysql_query("SET NAMES 'utf8'");
On the local test server (configured as in previous posts) where all the characters are displayed as they should, the script when viewed in a browser displays:
page_id: 261,
page_title: Добро пожаловать в Диксон,
hex: D094D0BED0B1D180D0BE20D0BFD0BED0B6D0B0D0BBD0BED0B2D0B0D182D18C20D0B220D094D0B8D0BAD181D0BED0BD,
length: 47,
char length: 25
page_id: 399,
page_title: Kelepçeler,
hex: 4B656C6570C3A7656C6572,
length: 11,
char length: 10
When I view the same script on the staging server I get:
page_id: 261,
page_title: ????? ?????????? ? ??????,
hex: D094D0BED0B1D180D0BE20D0BFD0BED0B6D0B0D0BBD0BED0B2D0B0D182D18C20D0B220D094D0B8D0BAD181D0BED0BD,
length: 47,
char length: 25
page_id: 399,
page_title: Kelep�eler,
hex: 4B656C6570C3A7656C6572,
length: 11,
char length: 10
Adding the line mysql_query("SET NAMES 'utf8'"); immediately following the connection on the staging server script gives:
page_id: 261,
page_title: Добро пожаловать в Диксон,
hex: D094D0BED0B1D180D0BE20D0BFD0BED0B6D0B0D0BBD0BED0B2D0B0D182D18C20D0B220D094D0B8D0BAD181D0BED0BD,
length: 47,
char length: 25
page_id: 399,
page_title: Kelepçeler,
hex: 4B656C6570C3A7656C6572,
length: 11,
char length: 10
It seems the hex, length and char length is the same in all instances.
Kind regards,
Chris