转换->;用于Perl中DBD::mysql的numrows
我有一些使用Perl中旧的转换->;用于Perl中DBD::mysql的numrows,mysql,perl,dbd,Mysql,Perl,Dbd,我有一些使用Perl中旧的Mysql库的旧代码 大多数情况下,更新代码以使用DBD::mysql不会有问题,但是我遇到了一个问题,->numrows不起作用 在使用DBD::mysql时,我应该如何获得相同的功能 使用Mysql use Mysql; $type = "yellow"; $statement = "select name from customer where type = '$type'"; $sth = $dbh->query($statement); if (
Mysql
库的旧代码
大多数情况下,更新代码以使用DBD::mysql
不会有问题,但是我遇到了一个问题,->numrows
不起作用
在使用DBD::mysql时,我应该如何获得相同的功能
使用Mysql
use Mysql;
$type = "yellow";
$statement = "select name from customer where type = '$type'";
$sth = $dbh->query($statement);
if ($sth->numrows) {
print "Success!"; # This does work.
}
use DBI;
use DBD::mysql;
$type = "yellow";
$statement = "select name from customer where type = ?";
$sth = $dbh->prepare($statement);
$sth->execute($type);
if ($sth->numrows) {
print "Success!"; # This doesn't work.
}
使用DBD::mysql
use Mysql;
$type = "yellow";
$statement = "select name from customer where type = '$type'";
$sth = $dbh->query($statement);
if ($sth->numrows) {
print "Success!"; # This does work.
}
use DBI;
use DBD::mysql;
$type = "yellow";
$statement = "select name from customer where type = ?";
$sth = $dbh->prepare($statement);
$sth->execute($type);
if ($sth->numrows) {
print "Success!"; # This doesn't work.
}
这就是我得到的错误:
tail/var/log/apache2/error.log
Can't locate object method "numrows" via package "DBI::st"
我认为你应该使用它如下
if ($sth->rows)
我认为你应该使用它如下
if ($sth->rows)
From:对于SELECT语句,execute只是在数据库引擎中“启动”查询。调用execute后,使用其中一个fetch方法检索数据。execute方法不返回查询将返回的行数(因为大多数数据库无法提前知道),它只返回一个真值$numRows=$sth->行;如果($numRows){print“Success!”}
这应该可以工作。From:对于SELECT语句,只需在数据库引擎中执行“启动”查询即可。调用execute后,使用其中一个fetch方法检索数据。execute方法不返回查询将返回的行数(因为大多数数据库无法提前知道),它只返回一个真值$numRows=$sth->行;如果($numRows){print“Success!”}这应该可以。