我在读取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有什么问题吗?
嗨,各位,很抱歉打扰你们。我已经找到了原因。这是一个低级未命中,因为我使用了多个$dbh,并且我把执行名称弄错了。
my $execute_A = $dbh->prepare($sql);
$execute_A->execute or die "Error: $DBI::errstr\n";
my @client = $execute_B->fetchrow_array() #$execute_B here when I copied lines and modified.你的帮助对我很重要。谢谢你们所有人。
发布于 2013-12-19 01:30:18
替换
my @client = $execute->fetchrow_array() or die "Error: $DBI::errstr\n";使用
my @client = $execute->fetchrow_array();您正在获取空数组,并且这不适合像..or die ..建议的那样进行错误检查。
.. or die ..只对prepare和execute方法有意义。
附注,您还缺乏适当的错误检查:
my $execute = $dbh->prepare($sql) or die $dbh->errstr; # not $DBI::errstr
$execute->execute or die $execute->errstr; # not $DBI::errstr另外,使用占位符来防止sql injection。
发布于 2013-12-19 01:13:26
尝试将sql查询中的变量$id更改为?然后通过execute传递变量。如$execute->execute($id)
发布于 2013-12-19 01:16:29
在我看来,execute会导致mysql服务器崩溃。这是我能想到的唯一可以解释“无法获取有关错误的信息”的原因。如果你使用的是safe_mysqld,你可能不会注意到,因为它会自动重启。
如果这是在一个网站上,你偶尔会收到这个错误,那么有人试图通过在你的表单中放入像3' or '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然后打电话给$execute->execute($id)。
https://stackoverflow.com/questions/20664164
复制相似问题