in reply to DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at

Please ignore, English cat was running over my keyboard and confusing me.

I don't think execute returns an error but a result object.

It's on this object you call fetch-methods not on the sth statement handle.

Edit

Yes see execute of DBI

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

  • Comment on Re: DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at

Replies are listed 'Best First'.
Re^2: DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at
by ysth (Canon) on Oct 29, 2024 at 14:00 UTC
    No, you use the statement handle. Reread the doc you linked.
    --
    A math joke: r = | |csc(θ)|+|sec(θ)| |-| |csc(θ)|-|sec(θ)| |
Re^2: DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at
by Digioso (Sexton) on Oct 29, 2024 at 14:05 UTC
    Thanks!

    I've already searched here and in other forums, but couldn't find a solution.

    But I've fixed it now by simply removing the loop.
    The select statement can only return one row.
    So instead of the loop I am simply writing that into a variable.

    my $ergebnis = $sth->fetchrow_hashref();

    Still I don't understand why using this in a while loop causes an error. oO
      My guess is it's called again after the successful call, but I can't test at the moment.

      The undef shouldn't allow this.

      I normally only use ->fetchall_hashref

      Update

      Did you show us how you checked on this error after the fetch?

      Or do you auto-raise them with RaiseError of DBI ?

      Update

      In general it's better to supply a SSCCE to reproduce the issue instead of all this less relevant CGI Code

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        Depending on the number of rows, fetching them all can by quite memory consuming. My code (although for DBD::Pg) usually looks like this:

        use DBI; use Carp; use English; ... my $dbh = DBI->connect($self->{dburl}, $self->{dbuser}, $self->{dbpass +word}, { AutoCommit => 0, # This is a proper + database, not a key-value store RaiseError => 0, # Child protection + mode off. We are doing our own error checking, thank you very much AutoInactiveDestroy => 1, # Just do + the right thing when using fork() }) or croak("$EVAL_ERROR"); ... # sub bla($dbh) { my $selsth = $dbh->prepare_cached("SELECT foo, bar, baz FROM bli WHERE + bla = ? ORDER BY blub") or croak($dbh->errstr); # If the statement is wrong, no point +of internal error handling if(!$selsth->execute("foobar")) { $reph->debuglog($dbh->errstr); $dbh->rollback; return 0; # Tell caller we failed } # Work on the data row-by-row while((my $line = $selsth->fetchrow_array)) { ... # Do something with the data ... } $selsth->finish; # Make sure we don't leave database pointers and stuf +f around $dbh->commit; # Finish the current transaction, unlock all locked tabl +es/rows and stuff. # commit() may or may not happen in THIS function, depen +ding on if the caller needs to # chain multiple functions in a single transaction return 1; # tell caller we succeeded. ...

        PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
        Also check out my sisters artwork and my weekly webcomics