2012-08-24 26 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()方法的調用,則返回的錯誤將會不同;錯誤消息將是關於讀取光標末尾或已取回最後一行的內容,或者是出現該錯誤的信息。)