I am building a My开发者_开发技巧sql database, holding details of owners and breeders of ponies, as well as details on the animals themselves. The information is updated and viewed via PHP. As part of this, I have pages where contacts are listed out, and other pages where contacts may be assigned (via dropdown) to an animal as either an owner or a breeder. The database connection is set in the header (called by function) and closed at the end of each query.
I noticed that some characters (e.g. É) were not being rendered properly (�) in the dropdown lists of contacts, but were fine in the list pages. Having looked into previous questons posed here (Special characters in PHP / MySQL) I set my table's collation to utf8_bin, and connect to the db via
<?php
$con = mysql_connect("localhost", "user", "pass");
mysql_select_db("db", $con);
mysql_set_charset('utf8',$con);
?>
This resolved the dropdown list issue perfectly - but the list pages are now causing trouble. (É is now É). My browser was set to Western ISO-8859-1, but even changing that to UTF-8 has not helped. Echoing out
$charset = mysql_client_encoding($con);
echo "The current character set is: $charset\n";
at the foot of the list returns uft8, as it should. My bam is boozled.
Any advice greatly appreciated!
(P.s. in anticipation: the language will be overwhelmingly English, but with some Irish Gaelic, and possibly some French & German too. I have also tried all of the above with the Latin1 encoding.)
EDIT:
To fix the problem:
- set the charset for the connection ($con)
- set the collation for the data in the table - by column, by table is not sufficient
- made sure to declare the HTML charset in my header via meta tag.
I suspect that you list display code utf8-encodes the HTML somehow. Remove that utf8_encode
calls there.
You might also be missing the encoding declaration in the HTML pages themselves, which is needed to tell the browser that the page has the specific charset.
精彩评论