From the DBI docs.
$hash_ref = $dbh->fetchall_hashref($key_field);
The $key_field parameter provides the name of the field that holds the value to be used for the key for the returned hash.
The $key_field parameter can also be specified as an integer column number (counting from 1). If $key_field doesn't match any column in the statement, as a name first then as a number, then an error is returned.
| [reply] [d/l] |
"fetchall_hashref" NEW
$hash_ref = $dbh->fetchall_hashref($key_field);
The "fetchall_hashref" method can be used to
fetch all the data to be returned from a pre
pared and executed statement handle. It returns
a reference to a hash that contains, at most,
one entry per row.
If there are no rows to return,
"fetchall_hashref" returns a reference to an
empty hash. If an error occurs,
"fetchall_hashref" returns the data fetched
thus far, which may be none. You should check
"$sth-">"err" afterwards (or use the "RaiseEr
ror" attribute) to discover if the data is com
plete or was truncated due to an error.
The $key_field parameter provides the name of
the field that holds the value to be used for
the key for the returned hash. For example:
$dbh->{FetchHashKeyName} = 'NAME_lc';
$sth = $dbh->prepare("SELECT FOO, BAR, ID,
NAME, BAZ FROM TABLE");
$hash_ref = $sth->fetchall_hashref('id');
The $key_field parameter can also be specified
as an integer column number (counting from 1).
If $key_field doesn't match any column in the
statement, as a name first then as a number,
then an error is returned.
This method is normally used only where the key
field value for each row is unique. If multi
ple rows are returned with the same value for
the key field then later rows overwrite earlier
ones.
The $key_field parameter can also be specified
as an integer column number (counting from 1).
If $key_field doesn't match any column in the
statement, as a name first then as a number,
then an error is returned.
This method is normally used only where the key
field value for each row is unique. If multi
ple rows are returned with the same value for
the key field then later rows overwrite earlier
ones.
print "Name for id 42 is $hash_ref->{42}->{name}\n";
The $key_field parameter can also be specified
as an integer column number (counting from 1).
If $key_field doesn't match any column in the
statement, as a name first then as a number,
then an error is returned.
This method is normally used only where the key
field value for each row is unique. If multi
ple rows are returned with the same value for
the key field then later rows overwrite earlier
ones.
You need a unique key from the data to index the hash.
After Compline, Zaxo | [reply] |
Do you have 'RaiseError' turned on for your database handle? If not, check each prepare() and execute() call for errors (printing out the error message from $DBI::errstr or $dbh->errstr if it's available). These functions return a false value when they fail. Don't ignore this. | [reply] [d/l] [select] |
What is the value of $query ? | [reply] |