1

I have some old code that uses the old Mysql library in Perl.

Most of the time updating the code to use DBD::mysql instead works with no problems however I have run into an issue where ->numrows does not work.

What should I do to get the same functionality whilst using DBD::mysql

use 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 DBD::mysql

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.
}

This is the error I get back:

tail /var/log/apache2/error.log

 Can't locate object method "numrows" via package "DBI::st"
Joseph
  • 3,899
  • 10
  • 33
  • 52
  • From [DBI](http://p3rl.org/DBI): For SELECT statements, execute simply "starts" the query within the database engine. Use one of the fetch methods to retrieve the data after calling execute. The execute method does not return the number of rows that will be returned by the query (because most databases can't tell in advance), it simply returns a true value. – choroba Jul 20 '16 at 08:44
  • 1
    `$sth->execute($type); $numRows = $sth->rows; if ($numRows) { print "Success!";}` This should work. – AbhiNickz Jul 20 '16 at 08:48

1 Answers1

0

I think you should use it as below

if ($sth->rows)
laojianke
  • 44
  • 4