gpt4 book ai didi

mysql - DBD::mysql fetchrow_array 失败:没有 execute() 的 fetch()?

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:44 27 4
gpt4 key购买 nike

我收到以下代码的错误:

#!C:/usr/bin/perl -w

use CGI;
use strict;
use DBI();
use CGI::Carp qw(fatalsToBrowser);


print "content-type: text/html; charset=iso-8859-1\n\n";

my $q=new CGI;
my $ename=$q->param('ename');
my $email=$q->param('email');

print $q->header;
print "<br>$ename<br>";


#connect to database.
my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost","root","mukesh",
{'RaiseError' => 1});

eval {$dbh->do("CREATE TABLE IF NOT EXISTS emp (ename VARCHAR(20), email VARCHAR(50))")};
print "<br>creating table emp failed: $@<br>" if $@;


my $sql="INSERT INTO emp(ename,email) values('$ename','$email')";

my $sth = $dbh->prepare($sql) or die "Can't prepare $sql:$dbh->errstrn";
#pass sql query to database handle


my $rv = $sth->execute() or die "can't execute the query: $sth->errstrn";

my @row;
while (@row = $sth->fetchrow_array) {
print join(", ",@row);
}

$sth->finish();
$dbh->disconnect();

if ($rv==1){
print "<br>Record has been successfully updated !!!<br>";
}else{
print "<br>Error!!while inserting record<br>";
exit;
}

请指教,请问是什么问题。如果我删除这段代码,它工作正常。

my @row;
while (@row = $sth->fetchrow_array) {
print join(", ",@row);
}

最佳答案

您正尝试在 DBI 中使用常见的 SELECT 循环 - 准备、执行、获取 - 用于非选择 查询(INSERT 在你的情况下)。您可能应该做的是直接检查 execute 的结果。引用 the doc :

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.

关于mysql - DBD::mysql fetchrow_array 失败:没有 execute() 的 fetch()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18816789/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com