February 2000
Intermediate to advanced
364 pages
11h 47m
English
When fetchall_arrayref( )
is invoked with no arguments, it
returns a reference to an array containing references to each
row in the result set. Each of those references refers to an array
containing the field values for that row. Figure 5.4 illustrates the data structure returned.
![]() |
This looks pretty convoluted, but it is, in fact, extremely simple to
access the data stored within the data structure. For example, the
following code shows how to dereference the data structure returned
by fetchall_arrayref( ) when run with no
arguments:
#!/usr/bin/perl -w # # ch05/fetchall_arrayref/ex1: Complete example that connects to a database, # executes a SQL statement, then fetches all the # data rows out into a data structure. This # structure is then traversed and printed. use DBI; ### The database handle my $dbh = DBI->connect( "dbi:Oracle:archaeo", "username", "password" , { RaiseError => 1 }); ### The statement handle my $sth = $dbh->prepare( " SELECT name, location, mapref FROM megaliths " ); ### Execute the statement $sth->execute( ); ### Fetch all the data into a Perl data structure my $array_ref = $sth->fetchall_arrayref( ); ### Traverse the data structure and dump each piece of data out ### ### For each row in the returned array reference ... foreach my $row (@$array_ref) { ### Split the row up and print each field ... my ( ...