生成两个表的有关程序

戻る

#!C:/perl/bin/perl # show_money_percent.pl use strict; use DBI; use CGI qw/:standard/; use HTML::Template; my ( %t, @rec, @loop1, @loop2 ); $t{template} = HTML::Template->new(filename => 'money_percent.htm'); $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; } # money table $t{sth} = $t{dbh}->prepare("select * from money"); $t{sth}->execute; @loop1 = (); while (@rec = $t{sth}->fetchrow_array) { my %row = ( id => $rec[0], English => $rec[1], Chinese => $rec[2] ); push(@loop1, \%row); } $t{sth}->finish; # percent table $t{sth} = $t{dbh}->prepare("select * from percent"); $t{sth}->execute; @loop2 = (); while (@rec = $t{sth}->fetchrow_array) { my %row = ( id => $rec[0], Rate => $rec[1] ); push(@loop2, \%row); } $t{sth}->finish; $t{dbh}->disconnect; $t{template}->param(LOOP1 => \@loop1); $t{template}->param(LOOP2 => \@loop2); print $t{template}->output; 1; -------------------------------------------------------------- <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>money_percent</title> <head> <body bgcolor="#fcf5ca"> <h2>money_percent</h2> <a href="http://localhost/index.html">http://localhost/index.html</a><br> <hr> <center> <table> <tr bgcolor="white"><th>ID</th><th>English</th><th>Chinese</th></tr> <TMPL_LOOP NAME="LOOP1"> <tr bgcolor="white"> <td><TMPL_VAR NAME="id"></td> <td><TMPL_VAR NAME="English"></td> <td><TMPL_VAR NAME="Chinese"></td> </tr> </TMPL_LOOP> </table> </center> <hr> <center> <table> <tr bgcolor="white"><th>ID</th><th>Rate</th></tr> <TMPL_LOOP NAME="LOOP2"> <tr bgcolor="white"> <td><TMPL_VAR NAME="id"></td> <td><TMPL_VAR NAME="Rate"></td> </tr> </TMPL_LOOP> </table> </center> <hr> </body> </html> -------------------------------------------------------------- DROP TABLE IF EXISTS money; CREATE TABLE money ( id INT AUTO_INCREMENT, English char(30), Chinese char(30), PRIMARY KEY (id) ); INSERT INTO money (English,Chinese) VALUES("JPY","日元"); INSERT INTO money (English,Chinese) VALUES("USD","美元"); INSERT INTO money (English,Chinese) VALUES("EUR","欧元"); INSERT INTO money (English,Chinese) VALUES("GBP","英镑"); INSERT INTO money (English,Chinese) VALUES("RMB","人民币"); -------------------------------------------------------------- DROP TABLE IF EXISTS percent; CREATE TABLE percent ( id INT AUTO_INCREMENT, Rate varchar(10), PRIMARY KEY (id) ); INSERT INTO percent (Rate) VALUES("1.05"); INSERT INTO percent (Rate) VALUES("1.10"); INSERT INTO percent (Rate) VALUES("1.20"); INSERT INTO percent (Rate) VALUES("1.30"); INSERT INTO percent (Rate) VALUES("1.50"); INSERT INTO percent (Rate) VALUES("150"); INSERT INTO percent (Rate) VALUES("250"); --------------------------------------------------------------
戻る