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

Hi Monks, I am having some trouble with a MySQL statement, or rather the fetchrow_hashref to get the data. It always returns the error message "DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at", but nevertheless the results are completely fine and as expected. I have no idea what is causing this error.
$error returns 1

My code:
my $dbh = DBI->connect("DBI:mysql:$db:$host", $user, $pw) || die; my $sql; my $sth; if($ENV{'REQUEST_METHOD'} eq 'GET') { my $daten; $daten = $ENV{'QUERY_STRING'}; my @formularfelder = split(/&/, $daten); my @formular = (); my $i=0; foreach my $feld (@formularfelder) { (my $name, my $value) = split(/=/, $feld); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/<!--(.|\n)*-->//g; $formular[$i] = $cgi->escapeHTML($name); $i++; $formular[$i] = $cgi->escapeHTML($value); $i++; } if(!defined $formular[0] || $formular[0] ne "dl" || ! defined $for +mular[1] || $formular[1] !~ /^\d+$/) { Navi::print_navi(": Nice try... :"); print qq{<div id="category">[ NICE TRY ]</div>}; print "<b>Nice try...</b>"; } else { $sql = qq{select p.category as category, p.titel as titel, p.b +eschreibung as beschreibung , c.werbung as werbung from movie_project +s as p join movie_categories as c on c.id = p.category WHERE p.id = ? +}; $sth = $dbh->prepare($sql) or die "Error: $DBI::errstr"; my $error = $sth->execute($formular[1]) or die "Error: $DBI::e +rrstr"; print "$error\n"; while (my $ergebnis = $sth->fetchrow_hashref()) {
  • Comment on DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at
  • Download Code

Replies are listed 'Best First'.
Re: DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at
by choroba (Cardinal) on Oct 29, 2024 at 13:49 UTC
    Have you search PerlMonks for similar questions? This one seems relevant and might even contain a solution: fetchrow_hashref failed problem.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at
by cavac (Prior) on Oct 30, 2024 at 08:46 UTC

    Why do you use $DBI::errstr? Oh, i see, the docs for DBD::mysql mix it up. Try looking at $dbh->errstr (as seen in the examples at the top of the documentation) instead to see if there's a difference.

    Did you

    1. try the statement directly in the mysql client?
    2. make sure no other transactions are currently locking the table?
    3. try $dbh->ping() as a basic test to see if your connection to the database actually works?
    4. check what $dbh->errstr shows directly after the DBI->connect() call?
    5. re-install/recompile DBD::mysql to make sure you are using the correct version of the C library?
    6. check the database logfiles? Is there anything in them that might hint at an error?

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
    Also check out my sisters artwork and my weekly webcomics
      Edit:
      Dang... I found the issue. I've reused $sth within the while loop...
      Thanks a lot for your help, everyone!
Re: DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at
by LanX (Saint) on Oct 29, 2024 at 13:55 UTC
    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

      No, you use the statement handle. Reread the doc you linked.
      --
      A math joke: r = | |csc(θ)|+|sec(θ)| |-| |csc(θ)|-|sec(θ)| |
      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