2012-08-24 25 views
1

我目前的Perl代码通过所有的数据库查询的行的运行,然后抛出一个Perl的非执行式提取错误 - 在执行

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

末。它几乎就像有些东西没有告诉循环停在行的末尾,但我已经像其他人那样写了它。

因此,例如:查询会拉起

shortcode1
shortcode2
shortcode3
则抛出错误这里

$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(); 
} 
+2

'while()'循环中'finish()'做了什么? – Mat

回答

6

的问题是,你正在处理多行从$sth

您的代码从$sth中获取一行,然后您的代码循环遍历从$shortcodeSTH开始的每一行,直到没有更多行。然后你的代码在$shortcodeSTH上调用finish()方法。 (这是规范模式,因为您已经提取了所有行)。

然后,您的代码第二次从外部循环开始,从$sth获取第二行。当您的代码尝试第二次尝试从$shortcodeSTH循环开始时,您已经获取了所有行并关闭了语句句柄。没有更多的行要检索。 (如果您还没有发出对finish()方法的调用,则返回的错误将会不同;错误消息将是关于读取光标末尾或已取回最后一行的内容,或者是出现该错误的信息。)