データ抽出1(fetchrow_array)

結果を配列に格納

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;
 
########################################
# データ取得方法1
########################################
# レコード数を取得
my $num_rows = $sth->rows;
print "該当 $num_rows 件\n";
 
# 配列に設定して取得
while ( my @arr_ref = $sth->fetchrow_array){
    # リスト変換
    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:22