A quick way to fetch and print
The DBI
supports a utility method called
dump_results( )
for fetching all of the rows in a statement handle’s result set
and printing them out. This method is invoked via a prepared and
executed statement handle, and proceeds to fetch and print all the
rows in the result set from the database. As each line is fetched, it
is formatted according either to default rules or to rules specified
by you in your program. Once dump_results( ) has
finished executing, it prints the number of rows fetched from the
database and any error message. It then returns with the number of
rows fetched.
For example, to quickly display the results of a query, you can write:
$sth = $dbh->prepare( "
SELECT name, mapref, location
FROM megaliths
" );
$sth->execute( );
$rows = $sth->dump_results( );which would display the following results:
'Balbirnie', 'NO 285 029', 'Balbirnie Park, Markinch, Fife' 'Castlerigg', 'NY 291 236', 'Near Keswick, Cumbria, England' 'Sunhoney', 'NJ 716 058', 'Near Insch, Aberdeenshire' 'Avebury', 'SU 103 700', 'Avebury, Wiltshire, England' 4 rows
You can customize the way in which this output is formatted by specifying the maximum length of each field within the row, the characters separating each field within the row, and the characters separating each row. You can also supply a Perl filehandle to which the output is written.
The default settings for these parameters are:
1: Maximum Field Length - 35 2: Line Separator - "\n" 3: Field Separator - "," 4: ...