Thread CGI + MySQL = UTF8-Problem (3 answers)
Opened by maral at 2012-12-10 22:54

maral
 2012-12-10 22:54
#164098 #164098
User since
2011-12-03
12 Artikel
BenutzerIn
[default_avatar]
In MySQL hab ich folgende Datenbank:

Code: (dl )
1
2
3
4
5
6
7
8
9
10
create database adressbuch 
CHARACTER SET = 'utf8'
COLLATE = 'utf8_general_ci';

CREATE TABLE `country` (
`country_id` varchar(2) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`iso3` varchar(3) DEFAULT NULL,
PRIMARY KEY (`country_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



Mit einem Apache Server hole ich mir die Daten per CGI


Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/perl -w

use strict;
use CGI qw(param);
use CGI::Carp qw(fatalsToBrowser set_message);
use DBI;
use Encode;
set_message('There is a problem in the script. 
Send me a mail to (<a href="mailto:xy@z.net">xy@z.net</a>), 
giving this error message and the time and date of the error.');

print "Content-type: text/html\n\n";


########################################################################
# program_name
########################################################################
my $script_name = $ENV{'SCRIPT_FILENAME'}; #get scriptname from cgi

if (defined($script_name) != 1) {
        # for test local
        $script_name = $0;      # get scriptname
}
$script_name =~ s/^.+\///g;


########################################################################
# parameter
########################################################################

our $param_table     = param('table');  # name of the table


$param_table = ''               unless defined $param_table;


my $sql_statment = '';
my $title = 'Adressbuch';
if ($param_table eq '') {
        $sql_statment = "SELECT table_name FROM information_schema.tables where table_schema='adressbuch';";
        $title .= ' - Alle Tabellen';
} else {
        $sql_statment = "SELECT * FROM ".$param_table.";";
        $title .= ' - Tabelle '.$param_table;
}

########################################################################
# Begin HTML
########################################################################

###################################################################################
# HTML Head, Title
###################################################################################
print <<HTML_HEAD;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="description" content="Adressenbuch" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>$title</title>
</head>
<body>
HTML_HEAD

print "<h1>".$title."</h1>";


my $dbh = DBI->connect( 'dbi:mysql:adressbuch',
                        'root',
                        '',
                      ) || die "Database connection not made: $DBI::errstr";


my $sth = $dbh->prepare($sql_statment) or die ($dbh->errstr);
$sth->execute or die $sth->errstr;


print "<table>";
while (my @array = $sth->fetchrow_array()) {
   print "<tr>";

   foreach (@array) {
                if ($param_table eq '') {
                        print "\t<td>".'<a href="'.$script_name.'?table='. $_ .'">'.$_.'</a>'."</td>\n";
                } else {
                        print "\t<td>", $_, "</td>\n";
                }
   }
   print "</tr>\n";
}
print "</table>";
$dbh->disconnect();

print '</body>';
print '</html>';



und bekomme dann aber

Code: (dl )
1
2
3
AL 	Albanien 	ALB
AT �sterreich AUT
CA Kanada CAN


Für Österreich auf der Webseite ein � für Ö.

In MySQL selber krieg ich aber:
Code: (dl )
1
2
3
4
5
6
7
8
9
localhost adressbuch>select * from country limit 3;
+------------+-------------+------+
| country_id | name | iso3 |
+------------+-------------+------+
| AL | Albanien | ALB |
| AT | Österreich | AUT |
| CA | Kanada | CAN |
+------------+-------------+------+
3 rows in set (0.00 sec)


An welcher Stelle wird das "Ö" verhunzt? Kann das an irgendeiner Apache Einstellung liegen? System: Ubuntu 12.04, perl v5.14.2, Apache/2.2.22 (Ubuntu), Firefox 17.01

Vielen Dank für Eure Hilfe. Gleich noch ein großes Lob an alle hier, hab schon viel aus dem Forum gelernt.

View full thread CGI + MySQL = UTF8-Problem