データ抽出2(fetchrow_arrayref)
結果を配列に格納
sample.pl
# プラグマ
use strict;
use warnings;
# DBI定義
use DBI;
my $user = 'root';
my $passwd = '';
my $dbname = 'test01';
my $host = 'localhost';
my $db = DBI->connect("DBI:mysql:$dbname:$host", $user, $passwd);
my $sth = $db->prepare("select * from table01");
$sth->execute;
########################################
# データ取得方法2
########################################
# レコード数を取得
my $num_rows = $sth->rows;
print "該当 $num_rows 件\n";
# 配列のリファレンスを取得
while ( my $arr_ref = $sth->fetchrow_arrayref ){
my ($code, $value, $biko) = @$arr_ref;
print "id=$code, name=$value memo=$biko \n";
}
$sth->finish;
$db->disconnect;
# 処理開始
BEGIN {
print "test-script-start\n";
}
# 処理終了
END {
print "test-script-end\n";
}
結果
>perl sample.pl
test-script-start
該当 3 件
id=1, name=aaaaa memo=
id=2, name=bbbbb memo=
id=3, name=ccccc memo=
test-script-end
>
最終更新:2012年01月21日 18:24