dgBP
dgBP

Reputation: 1691

Perl mysql, why does this fetchrow_array not work?

I have Perl code like so:

$pre = $dbh->prepare("select distinct skills from people");
$pre->execute;

my @preList;
while (my $row = $pre->fetchrow_array()) {
    push (@preList,$row);
}
print "Pre list: $_" foreach @preList;

When I try the exact same statement in sql it works perfectly, but when I run this program it prints out nothing (suggesting an empty @preList). I am probably doing something very stupid, but cannot see it. I have used this format for getting data before, which works perfectly. Can anyone see what I've done wrong?

Upvotes: 1

Views: 7775

Answers (4)

key_
key_

Reputation: 577

$pre = $dbh->prepare("select distinct skills from people");
$pre->execute;

my @preList;
while (my $row = $pre->fetch()) {
    push (@preList,$row);
}
foreach @preList{
    print "Pre list row: " . join(',',@$_);
}

or:

$pre = $dbh->prepare("select distinct skills from people");
$pre->execute;

my $preList = $pre->fetchall_arrayref();

foreach @$preList{
    print "Pre list row: " . join(',',@$_);
}

Upvotes: 2

daniel gratzer
daniel gratzer

Reputation: 53871

Right now you are doing, fetchrow_array which returns a list but you are trying to store it in a scalar, you can either change it to fetchrow_arrayref or change $row to @row, or change my $row to my ($row).

Upvotes: 7

ysth
ysth

Reputation: 98398

When you say:

while (my $row = $pre->fetchrow_array()) {

a value is returned from a row, assigned into $row, then tested for truth. As soon as a skills value that is false is returned, the loop will terminate, without putting that value in @preList. If the first row of results has a skills that is NULL or "" or "0", you will see the results you are seeing.

Changing to:

while (my ($row) = $pre->fetchrow_array()) {

(or using @row, as others have suggested), a value is still returned from each row, and assigned into $row, but instead of the value being tested for truth, the number of elements returned from fetchrow_array is tested for truth (and fetchrow_array returns an empty list when it runs out of result rows, so this number is 1 for each row of results and then 0 to terminate the loop). This happens because while() expects a scalar to test, so gives the assignment scalar context, and in perl, a list assignment in scalar context returns a count of elements on the right side of the assignment, specifically so that code such as this will just work.

Upvotes: 4

gcbenison
gcbenison

Reputation: 11963

You should try replacing fetchrow_array in your code with fetchrow_arrayref.

Upvotes: 0

Related Questions