【问题标题】:Perl fetch without execute error - at end of executionPerl fetch 没有执行错误 - 在执行结束时
【发布时间】:2012-08-20 09:45:11
【问题描述】:

我当前的 perl 代码正在运行数据库查询的所有行,然后抛出一个

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

最后。几乎就像有些东西没有告诉循环在行尾停止,但我和其他人一样写了它。

例如:查询会拉起

简码1
简码2
简码3
然后在这里抛出错误

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

【问题讨论】:

  • finish()while 循环中做了什么?

标签: mysql perl cgi


【解决方案1】:

问题是您正在处理来自$sth 的不止一行。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多