初心者だけどPerlが大好き

コードが世界を変える!

Perlで作成したデータベースのテーブルを全部表示します

db2.cgi とします

#!D:/xampp/perl/bin/perl
use CGI;
use DBI;
use utf8;
$q = new CGI;
print $q->header(-type =>'text/html',-charset => 'utf-8'),
$q->start_html(-title=>"Database Form"),
$q->h1(' テーブルを全部表示してみましょう');
$dbname = "trinity777";
$user = "root";
$passwd = "あなたのパスワードにしてね";
$tbname = "cgipm";
$host = "localhost";
$myname=$q ->param('name');
# DBに接続
$dbh=DBI->connect("DBI:mysql:$dbname; host=$host", $user, $passwd) 
or die $DBI::errstr;
$dbh->do("SET NAMES utf8");
# SQL実行
$sth = $dbh->prepare(" SELECT * FROM  $tbname ");
if(!$sth->execute){print "SQL失敗\n";exit;}
print $q->start_table({-border=>1}),
$q->start_Tr,
$q->th(['id', 'mtime','name','email','subject', 'comments','uploaded_file']);
while (@rec = $sth->fetchrow_array())
{
print $q->start_Tr,
$q->td([$rec[0],$rec[1],$rec[2],$rec[3],$rec[4],$rec[5],$rec[6]]),
}
# ステートメントハンドルクリア
$sth->finish;
# DB切断
$dbh->disconnect;
print $q->end_html;