user1620902
user1620902

Reputation: 81

Perl fetch without execute error - at end of execution

My current perl code is running through all of the rows of the database query and then throwing a

DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at ./recieveTxn.cgi 
line 168.

At the end. It almost is like there's something that's not telling the loop to stop at the end of the rows, but I have written it just as the others.

So for example: The query would pull up

shortcode1
shortcode2
shortcode3
then throw the error here

$sql = "
    SELECT
        aPI.folder AS aPIfolder, 
        aPI.rowNum AS aPIrowNum,
        hasInput,
        evalCode
    FROM
        aPersonalItems as aPI
    LEFT JOIN 
        pItems_special_inputs as SI 
    ON
        aPI.folder = SI.folder AND
        aPI.rowNum = SI.rowNum
    WHERE
        hasInput=1 AND
        aPI.folder='$FORM{'folder'}'
    ORDER BY
        aPI.rowNum
";

$sth = $dbh->prepare( $sql );
$sth->execute();

my ($shortcoderow, $row);
my $shortcodeSQL = "
    SELECT 
        * 
    FROM
        pItemsShortCodes 
    WHERE
        folder='$FORM{'folder'}' 
    ORDER BY
        rowNum
";
my $shortcodeSTH = $dbh->prepare( $shortcodeSQL );
$shortcodeSTH->execute();

while( my $ref = $sth->fetchrow_hashref() ) {

    my $shortCode;
    my $isblank = 1;
    my $rowNum  = $ref->{'aPIrowNum'};

    while(my $shortcodeRef = $shortcodeSTH->fetchrow_hashref())
    #&& $rowNum == $shortcodeRef->{'rowNum'}) #line 168 above
    {
        $shortCode=$shortcodeRef->{'shortcode'};
        print $shortCode."\n";
    }
    $shortcodeSTH->finish();
}

Upvotes: 1

Views: 1503

Answers (1)

spencer7593
spencer7593

Reputation: 108410

The problem is that you are processing more than one row from $sth.

Your code fetches a row from $sth, and then your code loops through every row from $shortcodeSTH, until there are no more rows. Then your code calls the finish() method on $shortcodeSTH. (Which is the normative pattern, since you've already fetched all the rows.)

Then, your code starts through the outer loop a second time, fetching a second row from $sth. When your code attempts to start through the $shortcodeSTH loop a second time, you've already fetched all of the rows and closed the statement handle. There aren't any more rows to retrieve. (The error returned would be different if you hadn't issued the call to the finish() method; the error message would be something about fetching past the end of the cursor, or already fetched last row, or something to that effect.)

Upvotes: 5

Related Questions