Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

DBI error

by hakkr (Chaplain)
on Sep 25, 2002 at 15:52 UTC ( [id://200651]=perlquestion: print w/replies, xml ) Need Help??

hakkr has asked for the wisdom of the Perl Monks concerning the following question:

Friendly Monks, when I do
my $sth = $dbh->prepare($query); $sth->execute(); my $stories=$sth->fetchall_hashref;
I get
DBI fetchall_hashref: invalid number of parameters: handle + 0

Can someone please decode this message for me as I am unaware of the fetchall_hashref method requiring any parameters

many thanks

Replies are listed 'Best First'.
Re: DBI error
by Abstraction (Friar) on Sep 25, 2002 at 16:55 UTC
    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.
Re: DBI error
by Zaxo (Archbishop) on Sep 25, 2002 at 17:00 UTC

    From the DBI pod:

           "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

Re: DBI error
by Fastolfe (Vicar) on Sep 25, 2002 at 16:07 UTC
    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.
Re: DBI error
by metadatum (Scribe) on Sep 25, 2002 at 16:00 UTC
    What is the value of $query ?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://200651]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found