boboson has asked for the wisdom of the Perl Monks concerning the following question:
before I go mad, I'll try to post a question here:
The Swedish characters from my mysql database doesn't display correctly on my webpage.
I use a MySQL 5.0.32 database with the following perl modules: CGI::Application, DBI, HTML::Template etc.
The servers character set is latin1. I have the following line in my .htacces file:
AddCharset UTF-8 html
This line doesn't do much more than set my html files to utf-8, my templatefiles are with the extension phtml, and these files are outputed through index.cgi
I have set the meta tag in my template files to:
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w +3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="se" xml:lang="se"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" + />
In my CGI::Application BaseClass i have the following code:
use utf8; # to state that the script itself is in utf8 binmode STDIN, ":encoding(utf8)"; binmode STDOUT, ":encoding(utf8)"; use as_utf8; # which is the hack posted at http://www.perlmonks.org/?n +ode_id=651574
My connection to the database looks like this:
# Connect to the database my $dbh = DBI->connect($dsn, $user, $password, { RaiseError => 1, Auto +Commit => 0, mysql_enable_utf8 => 1 }) or die "Can't connect: ", $DBI::errstr; my $sql = qq{SET NAMES 'utf8' COLLATE 'utf8_swedish_ci';}; $dbh->do($sql);
SHOW VARIABLES LIKE 'c%' gives me the following output: Variable_name Value character_set_client utf8 character_set_connection utf8 character_set_database utf8 character_set_filesystem binary character_set_results utf8 character_set_server latin1 character_set_system utf8 character_sets_dir /usr/share/mysql/charsets/ collation_connection utf8_unicode_ci collation_database utf8_swedish_ci collation_server latin1_swedish_ci completion_type 0 concurrent_insert 1 connect_timeout 5
When I try to output the following text:
Törjebjöåärne Ålandssäöna
which was inserted from my perl code, I'll get the following output on my webpage:
Törjebjöåärne Ålandssäöna
If I decode the mysql output:
use Encode; decode_utf8($tmpl->output);
The mysql data will show correctly, but my template files will show with the reverse questionmark sign.
I guess I could decode the data from my database and it would all work, but not all data should be decoded, just the varchar and text fields.
What is the correct way of doing this?
What am I missing?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: UTF-8 webpage output from MySQL
by moritz (Cardinal) on Jan 22, 2008 at 12:09 UTC | |
by boboson (Monk) on Jan 22, 2008 at 12:24 UTC | |
by moritz (Cardinal) on Jan 22, 2008 at 12:35 UTC | |
by boboson (Monk) on Jan 22, 2008 at 13:44 UTC | |
by moritz (Cardinal) on Jan 22, 2008 at 14:11 UTC | |
| |
|
Re: UTF-8 webpage output from MySQL
by Joost (Canon) on Jan 22, 2008 at 13:22 UTC | |
|
Re: UTF-8 webpage output from MySQL
by KurtSchwind (Chaplain) on Jan 22, 2008 at 14:02 UTC | |
by boboson (Monk) on Jan 22, 2008 at 14:26 UTC | |
|
Re: UTF-8 webpage output from MySQL
by Anonymous Monk on Jan 22, 2008 at 23:22 UTC | |
by moritz (Cardinal) on Jan 23, 2008 at 06:48 UTC | |
by Anonymous Monk on Jan 23, 2008 at 10:21 UTC | |
by moritz (Cardinal) on Jan 23, 2008 at 10:34 UTC | |
by boboson (Monk) on Jan 23, 2008 at 14:27 UTC | |
|