CGI プログラム5

戻る

#!C:/perl/bin/perl # show_parts1 use strict; use DBI; use CGI qw/:standard/; use HTML::Template; my ( %t, $n, @loop, @rec ); $t{template} = HTML::Template->new(filename => 'parts1.htm'); @loop = (); # initialize an array to hold your loop $t{dsn} = "DBI:mysql:host=localhost;database=cookbook"; $t{dbh} = DBI->connect($t{dsn}, "cbuser", "cbpass") or die "Cannot connect to server\n"; $t{dbh}->do("SET NAMES utf8"); if(!$t{dbh}){ print "SQL read ERROR!\n"; exit; } $t{sth} = $t{dbh}->prepare("select id, name,code,engine,DWG,memo from parts1"); $t{sth}->execute; $t{NO} = -1; while (@rec = $t{sth}->fetchrow_array) { $t{NO}++; @{ $t{list}[$t{NO}] } = @rec; } $t{sth}->finish; for $n ( 0 .. $#{ $t{list} } ) { # エンジン番号からエンジン名を抽出 $t{engine_name} = $t{dbh}->selectrow_array("select name from main_type1 where id = $t{list}[$n][3]"); my %row = ( id => $t{list}[$n][0], name => $t{list}[$n][1], code => $t{list}[$n][2], engine => $t{engine_name}, DWG => $t{list}[$n][4], memo => $t{list}[$n][5] ); # put this row into the loop by reference push(@loop, \%row); } $t{dbh}->disconnect; $t{template}->param(THIS_LOOP => \@loop); # send the obligatory Content-Type and print the template output print $t{template}->output; 1;
戻る