Ron Goral wrote: > > From: Rob Dixon [mailto:[EMAIL PROTECTED] > Sent: Saturday, January 03, 2004 9:51 AM > To: [EMAIL PROTECTED] > Subject: Re: fetchall_hashref error: invalid number of parameters? > > > > > > Ron Goral wrote: > > > > > > I am trying to hit a MySQL database using DBI::mysql. I am trying to get > > > table information using the SQL - "SHOW TABLE STATUS". > > > > > > The statement handle prepares and then executes as it should, but when I > > try > > > to get the info via $sth->fetchall_hashref, I get the following error: > > > > > > ================================================= > > > DBI fetchall_hashref: invalid number of parameters: handle + 0 Usage: > > > $h->fetchall_hashref() > > > ================================================= > > > > > > I'm not sure what form the data will be in when this statement is > > executed, > > > but the hashref is a hopeful thing. > > > > > > Here is the code after a good connection to the database - > > > > > > ================================================= > > > eval{$sth = $dbh->prepare("SHOW TABLE STATUS");}; > > > print qq[Error - $@ <br />] if $@; # No error here > > > > > > eval{$sth->execute();}; > > > print qq[Error - $@ <br />] if $@; # No error here > > > > > > my $ar_TableInfo; > > > eval{$ar_TableInfo = $sth->fetchall_hashref;}; > > > print qq[Error - $@ <br />] if $@; # Error occurs here > > > > > > print qq[About to iterate the array of hashrefs.....<br />]; > > > foreach my $hr_TableInfo (@$ar_TableInfo) > > > > You're getting confused here. $ar_TableInfo is a hashref > > so you can't dereference it as an array. > > > > > { > > > foreach my $key (keys %{$hr_TableInfo}) > > > {print qq[$key is $hr_TableInfo->{$key}<br />];} > > > } > > > ================================================= > > > > Like it says, you've supplied the wrong number of parameters > > to the fetchall_hashref method. You need to pass a single > > parameter saying which of the columns you want to use as the > > keys of the returned hash. In this case it makes little sense > > to use anything but the 'Name' column of the results, so you > > could write something like: > > > > my $hr_TableInfo = $sth->fetchall_hashref('Name'); > > > > foreach my $name (keys %{$hr_TableInfo}) { > > my $rows = $hr_TableInfo->{$name}{Rows}; > > my $update_time = $hr_TableInfo->{$name}{Update_time}; > > } > > > > depending on what information you need from the results. > > fetchall_hashref is an array ref of hash refs (one hash ref per row > contained in an array ref), so I have to dereference and iterate the array > ref to get to the hash refs underneath. The DBI Manpage says: > > "It returns a reference to an array that contains one hash of field name and > value pairs per row." > > Specifying column names is, unfortunately, an optional parameter for > fetchall_arrayref not fetchall_hashref. =(
Hi Ron. There's something odd somewhere. This is straight from perldoc DBI "fetchall_hashref" $hash_ref = $sth->fetchall_hashref($key_field); The "fetchall_hashref" method can be used to fetch all the data to be returned from a prepared and executed statement handle. It returns a reference to a hash that contains, at most, one entry per row. Try running: use DBI; print $DBI::VERSION, "\n"; I'm running DBI version 1.37 (But the POD in the DBI.pm source file claims that it's for version 1.34!!) I can't really help any further :-/ Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>