dbi - How to use fetchrow_arrayref correctly in perl script? -
i got stuck in usage of 'fetchrow_arrayref' in perl script. can point out i'm going wrong in script? i'd appreciate whatever inform me. thank you.
the problems i'm facing are:
(1) print $id; <-this doesn't print content of $id.
(2) print "$list[1]"; <-this prints array(0x8da6978) instead of actual content.
(3) reverse(@list); <-this doesn't reverse contents of @list.
(4) print "@{$_} \n"; <- "\n" doesn't work. why need @{}?
(5) print "\n"; <-this doesn't work well.
(6) print "@list"; <-this prints array(0x8da6978).
(7) print dumper(@inverse); <-prints fine, contents of array not reversed.
#!/usr/bin/perl use strict; use warnings; use dbi; use data::dumper; .... $dbh = dbi->connect($dbname, $dbuser, $dbpassword) || die "error $dbi::errstr"; $sth = $dbh->prepare("select * name id = 11"); $sth->execute; @list = (); while(my $element = $sth->fetchrow_arrayref){ push(@list, $element); } $sth->finish; $dbh->disconnect; ($id, $name, $email, $telephone) = @list; print "content-type: text/html; charset=utf-8\n\n"; print $id; (problem 1) print "$list[1]"; (problem 2) @inverse = reverse(@list); (problem 3) foreach (@inverse){ print "@{$_} \n"; (problem 4) } print "\n"; (problem 5) print "@list"; (problem 6) print dumper(@inverse); (problem 7) exit;
each item in list reference array containing data 1 row of database table.
this:
my ($id, $name, $email, $telephone) = @list;
appears trying process 1 row of table.
you need each member of @list
, not @list
itself.
for $row (@list) { ($id, $name, $email, $telephone) = @$row; print $id; }
Comments
Post a Comment