문제

Hi I follow the OTRS developer manual for using sql queries inside the modules.

I run the following query but I only get first value from result.

  my $SQL = "SELECT id FROM roles ";

  $Self->{DBObject}->Prepare(SQL => $SQL, Limit => 15);

  my @Row = $Self->{DBObject}->FetchrowArray();

if I check the size of @Row array I get one but in reality I have many roles created in roles table.

Can some one tell me whats missing ?

도움이 되었습니까?

해결책

I can't add the code snippet in the comment to the other answer but here goes; FetchrowArray() will return a list of columns for one row of the result, so you'd have to iterate over it like below; as pointed out. And if you want to add all results to an array, just use push.

$Self->{DBObject}->Prepare(
    SQL   => "SELECT id FROM table",
    Limit => 10
);

my @IDs;
while ( my @Row = $Self->{DBObject}->FetchrowArray() ) {
    push @IDs, $Row[0];
}

다른 팁

FetchrowArray() should be in a loop. This is an example from the OTRS doc:

$DBObject->Prepare(
    SQL   => "SELECT id, name FROM table",
    Limit => 10
);

while (my @Row = $DBObject->FetchrowArray()) {
    print "$Row[0]:$Row[1]\n";
}

I believe you should use SelectAll instead. SelectAll() return an array reference, not an array.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top