CGIをPerlで書いて、MariaDBからデータを抽出して、HTML出力した場合、なぜか文字化けします。
CGIファイルは、UTF-8です。
MariaDBに対して、「show variables like 'char%'」を打つと、下のようになります。
character_set_client : utf8mb4
character_set_connection : utf8mb4
character_set_database : utf8mb4
character_set_filesystem : binary
character_set_results : utf8mb4
character_set_server : utf8mb4
character_set_system : utf8
character_sets_dir : /usr/share/mysql/charsets/
character_set_filesystem 以外、utf8の方がよさそうですが、インターネットのサーバー上にあるため、my.iniファイルが変更する方法がわかりません。
クライアント側の環境:Windows11
サーバー側の環境: Linux
サーバの種類 : MariaDB
サーバのバージョン: 10.5.13-MariaDB-log - MariaDB Server
プロトコル バージョン : 10
ウェブサーバ : Apache
データベースクライアントのバージョン: libmysql - 10.1.48-MariaDB
CGIはHTMLファイルのテンプレートを読み込んで、データを埋め込む形にしています。
エラーとしては、「[sample.cgi:206:warn] Wide character in print at sample.cgi line 206.」とWeb上で表示されています。
206行目は、HTMLタグにデータを埋め込んだ変数をprintしています。
なお、DBのデータだけでなく、data.plファイルをrequireしてsample.cgiの変数に格納してHTML出力していますが、こちらの文字列も文字化けしています。data.plファイルも、UTF-8です。
plファイルの中身は、設定ファイルになっており、web siteの名前とか、site管理者の名前などがハッシュに格納されています。
大体が、「ãÂÂãÂÂã°」という感じで表示されています。
+------+------------+----+----+-------+----------------+
|Field -|Type -------|Null| Key|Default| Extra |
+------+------------+----+----+-------+----------------+
|id |int(6): | NO | PRI| NULL | auto_increment|
+------+------------+----+----+-------+----------------+
|t_bi |datetime | NO | | NULL | |
+------+------------+----+----+-------+----------------+
|ca_no |int(2) | NO | | NULL | |
+------+------------+----+----+-------+----------------+
|title |varchar(400)| NO | | NULL | |
+------+------------+----+----+-------+----------------+
|kizi |text | NO | | NULL | |
+------+------------+----+----+-------+----------------+
|k_bi |datetime |YES | | NULL | |
+------+------------+----+----+-------+----------------+
|iine |int(10) | NO | | 0 | |
+------+------------+----+----+-------+----------------+
|user |varchar(100)| NO | | NULL | |
+------+------------+----+----+-------+----------------+
perl
use strict; use DBI; require 'data.pl'; require 'db.pl'; my %out = &out_set; my %in = &parse_deco; my $data_source = $out{"data_source"}; my $username = $out{"username"}; my $password = $out{"password"}; my $user_nm = $in{"user_name"}; my $categpry = 1; # DB接続 my $dbh = DBI->connect($data_source, $username, $password,{mysql_enable_utf8 => 1}) or die $DBI::errstr; my $sth = $dbh->prepare(qq{ SELECT * FROM blog WHERE user = '$user_nm' AND ca_no = '$categpry' ORDER BY id DESC LIMIT 1 }); $sth->execute; my %row; my $i = 0; while (my $ary_ref = $sth->fetchrow_arrayref){ ($row{"id_$i"},$row{"t_bi_$i"},$row{"ca_no_$i"},$row{"title_$i"},$row{"kizi_$i"},$row{"k_bi_$i"},$row{"iine_$i"},$row{"user_$i"}) = @$ary_ref; $i++; } $sth->finish; my $sth2 = $dbh->prepare(qq{ SELECT t_bi FROM blog WHERE user = '$user_nm' AND ca_no = '$categpry' }); $sth2->execute; my @tourokubi = (); my $t = 0; while (my $ary_ref = $sth2->fetchrow_arrayref){ ($tourokubi[$t]) = @$ary_ref; $t++; } $sth2->finish; my $sth3; my ($yaer,$month); my %month; foreach(@tourokubi){ $_ =~ /(\d\d\d\d)-(\d\d)-(\d\d)/; $yaer = $1; $month = $2; $sth3 = $dbh->prepare(qq{ SELECT COUNT(*) FROM blog WHERE user = '$user_nm' AND ca_no = '$categpry' AND t_bi LIKE '%$yaer-$month%' }); $sth3->execute; while (my $ary_ref = $sth3->fetchrow_arrayref){ ($month{"$yaer-$month"}) = @$ary_ref; $t++; } $sth3->finish; $sth3 = ""; } # DB切断 $dbh->disconnect; # HTML表示 print <<"TAG"; Content-type:text/html <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>DB test</title> </head> <body> TAG my $key; foreach $key( keys %row){ print qq|$key = $row{"$key"}<br>\n|; } my $key2; my $yyyymm; foreach $key2( sort keys %month){ $yyyymm = $key2; $yyyymm =~ s/(\d\d\d\d)-(\d\d)/$1年$2月/; print "$yyyymm($month{$key2})<br>\n"; # count } print <<"TAG2"; </body> </html> TAG2 exit;
まだ回答がついていません
会員登録して回答してみよう