Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fetch DB results into an array, not a row array

Status
Not open for further replies.

CherylD

Programmer
May 1, 2001
107
CA
I'm wondering if there is a way to fetch the results of a query into an array. Not the row results in an array, but the whole result set. The SQL query is only pulling one field.

The reason I want to do this is I want to get all the ID fields and then put that into an array to then be able to randomly choose an item.

Any suggestions....
 
my $sql = "SELECT ACCOUNTID " .
"FROM sysdba.ACCOUNT " .
"WHERE (TYPE = 'Agent')";
$agentarray = $dbh->selectall_arrayref($sql);
my $index = rand @agentarray;
$parentid = $agentarray[$index];

I'm trying this but, $parentid is always blank.
 
[tt]selectall_arrayref[/tt] returns a reference to an array, which you're assigning properly to the scalar [tt]$agentarray[/tt]. You can't treat that as an array directly, though, so:

[tt]my $index = rand @agentarray;
$parentid = $agentarray[$index];
[/tt]

should be:

[tt]my $index = rand @{$agentarray};
$parentid = $agentarray->[$index];
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top