Database Reference
In-Depth Information
You can also invoke selectrow_array() in scalar context, in which case it returns only
the first column from the row (especially convenient for statements that return a single
value):
my $buddy_count = $dbh -> selectrow_array ( "SELECT COUNT(*) FROM profile" );
If a statement returns no result, selectrow_array() returns an empty array or undef ,
depending on whether you call it in array or scalar context.
selectrow_arrayref() and selectrow_hashref() select the first row of the result set
and return a reference to it, or undef if no row was selected. To access the column values,
treat the reference the same way you treat the return value from fetchrow_arrayr
ef() or fetchrow_hashref() . The reference also provides the row and column counts:
my $ref = $dbh -> selectrow_arrayref ( $stmt );
my $ncols = defined ( $ref ) ? @ { $ref } : 0 ;
my $nrows = $ncols ? 1 : 0 ;
my $ref = $dbh -> selectrow_hashref ( $stmt );
my $ncols = defined ( $ref ) ? keys ( % { $ref }) : 0 ;
my $nrows = $ncols ? 1 : 0 ;
selectcol_arrayref() returns a reference to a single-column array representing the
first column of the result set. Assuming a non- undef return value, access elements of
the array as $ref->[ i ] for the value from row i . The number of rows is the number of
elements in the array, and the column count is 1 or 0:
my $ref = $dbh -> selectcol_arrayref ( $stmt );
my $nrows = defined ( $ref ) ? @ { $ref } : 0 ;
my $ncols = $nrows ? 1 : 0 ;
selectall_arrayref() returns a reference to an array containing an element for each
row of the result. Each element is a reference to an array. To access row i of the result
set, use $ref->[ i ] to get a reference to the row. Then treat the row reference the same
way as a return value from fetchrow_arrayref() to access individual column values
in the row. The result set row and column counts are available as follows:
my $ref = $dbh -> selectall_arrayref ( $stmt );
my $nrows = defined ( $ref ) ? @ { $ref } : 0 ;
my $ncols = $nrows ? @ { $ref -> [ 0 ]} : 0 ;
selectall_hashref() returns a reference to a hash, each element of which is a hash
reference to a row of the result. To call it, specify an argument that indicates which
column to use for hash keys. For example, if you retrieve rows from the profile table,
the primary key is the id column:
my $ref = $dbh -> selectall_hashref ( "SELECT * FROM profile" , "id" );
Access rows using the keys of the hash. For a row that has a key column value of 12 , the
hash reference for the row is $ref->{12} . That row value is keyed on column names,
Search WWH ::




Custom Search