Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Perl DBD fetchrow_数组错误:_Mysql_Perl - Fatal编程技术网

Mysql Perl DBD fetchrow_数组错误:

Mysql Perl DBD fetchrow_数组错误:,mysql,perl,Mysql,Perl,我在读MySQL时遇到了一些奇怪的错误,我的代码是这样的: my $sql = <<"sqleof"; select t1.name t1_name, t2.name t2_name from t2 inner join t1 using(Id) where t2.Id in (select Id from t3 where t3Id='$id') sqleof #here $dbh had connected correctly and done some que

我在读MySQL时遇到了一些奇怪的错误,我的代码是这样的:

my $sql = <<"sqleof";
select t1.name t1_name, t2.name t2_name from t2
    inner join t1 using(Id)
    where t2.Id in (select Id from t3 where t3Id='$id') 
sqleof
#here $dbh had connected correctly and done some query before this; $sql can execute pretty well on MySQL command line and return me some records.
my $execute = $dbh->prepare($sql);
$execute->execute or die "Error: $DBI::errstr\n";
my @client = $execute->fetchrow_array() or die "Error: $DBI::errstr\n";
#here I got error saying: DBD::ODBC::st fetchrow_array failed:     Unable to fetch information about the error

你的帮助对我很重要。谢谢大家。

尝试将sql查询中的变量$id更改为?然后通过execute传递变量。就像$execute->execute($id)

在我看来,execute会导致mysql服务器崩溃。这是我能想到的解释“无法获取有关错误的信息”的唯一原因。如果您使用的是safe_mysqld,您甚至可能不会注意到,因为它会自动重新启动

如果这是在一个网站上,并且你偶尔会遇到这个错误,那么有人试图通过在你的表单中添加
3'或'a'='a
之类的内容来攻击你(试试如果你在问题中用这个字符串替换id会发生什么情况)。如果格式错误,服务器可能会尝试执行任何操作。要防止这种SQL注入攻击,请将查询重写为

my $sql = <<"sqleof";
select t1.name t1_name, t2.name t2_name from t2
    inner join t1 using(Id)
    where t2.Id in (select Id from t3 where t3Id=?) 
sqleof
my$sql=Replace

my @client = $execute->fetchrow_array() or die "Error: $DBI::errstr\n";

您正在获取空数组,这不适合进行错误检查,正如
或die..
所建议的那样

。。或死亡..
仅适用于
准备
执行
方法


另外,您还缺少正确的错误检查:

my $execute = $dbh->prepare($sql) or die $dbh->errstr; # not $DBI::errstr
$execute->execute or die $execute->errstr;             # not $DBI::errstr

另外,使用sql占位符来防止。

不应该对
fetch
方法执行错误检查,这一点并不完全正确(但不使用
或die
是正确的)。“如果没有更多行或发生错误,则fetchrow\u数组返回一个空列表。您应该在之后检查$sth->err(或使用RaiseError属性),以发现返回的空列表是否是由于错误造成的。”(强调我的)@但在使用多个DBI实例时意义不大。我复制并在命令行上运行$sql,可以返回5条记录。因此,我不知道为什么要获取空数组。$sth->err返回“1”,而$DBI::errstr返回无法获取有关错误的信息。对我来说似乎没有更多有用的消息了。@anaconda\u wly查询是否会在命令行上返回一些内容?尝试
$dbh->{RaiseError}=1
用于自动错误检查。您标记了mysql
,错误表明您正在将其与ODBC一起使用,这是一种非常不寻常的设置。我使用Windows ODBC数据源管理员创建的DSN,DSN使用mysql ODBC连接器连接远程数据库。你的评论对我很有价值。
my @client = $execute->fetchrow_array();
my $execute = $dbh->prepare($sql) or die $dbh->errstr; # not $DBI::errstr
$execute->execute or die $execute->errstr;             # not $DBI::errstr