给出错误 DBD::mysql::st fetchrow_array failed: fetch() without execute() only for DELETE statement

Gives error DBD::mysql::st fetchrow_array failed: fetch() without execute() only for DELETE statement

这是数据库连接,

$DBH = DBI->connect("dbi:mysql:$DATABASE","$USER","$PASS",{ RaiseError => 1, AutoCommit => 1 }) or die "Connection Error: $DBI::errstr\n";

和下面的代码,

$sth=$DBH->prepare("DELETE FROM sample where id=1 ") or warn $DBH->errstr;
$sth->execute or die "can't execute the query: $sth->errstr";
while(@row = $sth->fetchrow_array()){
                $count+=$sth->rows;
}

以上代码报错,

DBD::mysql::st fetchrow_array failed: fetch() without execute()..

但是,当我使用 select * from sample where id=1 时它会运行。它只给我 delete statement 的错误。为什么这样? 帮助 !谢谢!

您不能使用删除指令执行取行,它仅用于检索数据。
您认为应该 return ?

来自 DBI 文档

For a non-SELECT statement, execute returns the number of rows affected, if known. If no rows were affected, then execute returns "0E0", which Perl will treat as 0 but will regard as true. Note that it is not an error for no rows to be affected by a statement. If the number of rows affected is not known, then execute returns -1.

您不需要执行获取行,只需:

my $affected_row = $sth->execute or die "can't execute the query: $sth->errstr";