【问题标题】:DBD::mysql::st fetchrow_array failed: fetch() without execute()DBD::mysql::st fetchrow_array 失败:没有 execute() 的 fetch()
【发布时间】:2021-04-06 22:00:06
【问题描述】:

fetchrow_hashref 工作正常,但是当我使用 fetchrow_array 时出现以下错误。

#!/usr/bin/perl

use warnings;
use DBI;

$DB_name    = 'database';
$DB_user    = 'root';
$DB_pwd     = '';
my $dsn = 'dbi:mysql:avm:localhost:3306';

$dbh = DBI->connect($dsn,"$DB_user","$DB_pwd");

print "\nConnection error: $DBI::errstr\n\n";

$sth  = $dbh->prepare("SELECT * FROM tblmanufacturer");
$sth->execute();

while ( ($id,$name) = $sth->fetchrow_array() ) 
{
        print "$id\t\t $name \n";
}

$sth->finish();

$dbh->disconnect();

DBD::mysql::st fetchrow_array 失败:fetch() 没有 execute() 在

【问题讨论】:

    标签: perl


    【解决方案1】:

    我总是在“执行”和“准备”出现错误时使用“死”。

    $sql = $dbh->prepare( $query ) or die "Unable to prepare $query" . $dbh->errstr;
    $sql->execute() or die "Unable to execute '$query'.  " . $sql->errstr;
    

    【讨论】:

      【解决方案2】:

      检查execute() 和/或print "$DBI::errstr\n\n" 的返回值,看看执行是否失败。

      print $sth->execute(),"\n";
      

      【讨论】:

      • this 167, For print $sth->execute(),"\n";和使用未初始化的值 $DBI::errstr 打印 "$DBI::errstr\n\n"
      • 看看@mmrtnt 的回答,看起来比我的更值得。
      【解决方案3】:

      另一种方法是使用错误处理程序捕获错误,对它们执行任何您需要的操作(将其发送到您的日志文件、打印它们、终止或继续执行脚本)。
      这消除了在每个方法之后 " or die() " 的需要。关于HandleError 方法的文档可以在here 找到。

      首先看这个简单的例子:

      #!/usr/bin/perl
      
      use strict;
      use warnings;
      use DBI;
      
      my $DB_name    = 'database';
      my $DB_user    = 'root';
      my $DB_pwd     = '';
      my $dsn = 'dbi:mysql:avm:localhost:3306';
      my ($sth, $id, $name);
      
      my $dbh = DBI->connect($dsn,$DB_user,$DB_pwd, { PrintError => 0, ShowErrorStatement => 1, HandleError => \&dbi_error_handler,} );
      
      $sth  = $dbh->prepare("SELECT * FROM tblmanufacturer");
      $sth->execute();
      
      while ( ($id,$name) = $sth->fetchrow_array() ) 
      {
              print "$id\t\t $name \n";
      }
      
      $sth->finish();
      $dbh->disconnect();
      
      sub dbi_error_handler
      {
          my( $message, $handle, $first_value ) = @_;
          # print to your log file, call your own logger etc ... 
          # here it will die() to be similar to "or die()" method, but the line number is incorect
          die($message);
          # if you return false it will check/execute RaiseError and PrintError
          return 1;
      }
      

      附:没有理由在这里将您的字符串变量封装在引号中:($dsn,"$DB_user","$DB_pwd");,不要这样做,有关此的更多信息,请阅读this

      【讨论】:

        【解决方案4】:

        我在 apache fcgid 中遇到了这个错误,因为在我完成之前重用了 $sth。我只在 apache 错误日志中看到它。在准备和执行语句之后放入“或死......”没有任何帮助,并且脚本似乎无论如何都可以正常工作(在正常使用中只提取了一行,但有可能更多),因为它已经完成了遇到错误之前的预期。 错误只是出现在 Apache 日志中。 简单的修复,将 $sth 重命名为 $osth,问题就消失了。 如果您看到此错误并重新使用语句句柄,请尝试使用唯一的语句句柄来查看问题是否消失。

        # rename to $ostmt
        my $stmt="SELECT ...";
        # rename to $osth
        my $sth=$dbh->prepare($stmt) or die "Unable to prepare $stmt" . $dbh->errstr;
        $sth->execute() or die "Unable to execute '$stmt'.  " . $sth->errstr;
        while( my ( $f1, $f2 ... ) = $sth->fetchrow_array() ){
          # redeclare $stmt and $sth below using my $stmt, my $sth
          $stmt="SELECT ...";
          $sth=$dbh->prepare($stmt) or die "Unable to prepare $stmt" . $dbh->errstr;
          $sth->execute($f1) or die "Unable to execute '$stmt'.  " . $sth->errstr;
          my ( @stuff ) = $sth->fetchrow_array();
          # ...
          # do lots of stuff
          # ...
          # output fcgi content
        } # error kicks in here
        

        它比上面的示例涉及更多,但错误非常无用,所以我留下这个答案以防它帮助其他人。

        【讨论】:

          猜你喜欢
          • 2013-09-19
          • 1970-01-01
          • 1970-01-01
          • 2023-04-11
          • 2020-06-01
          • 2014-07-14
          • 2016-04-11
          • 1970-01-01
          • 2019-07-07
          相关资源
          最近更新 更多