I am querying a MySQL database with Perl DBI, and my goal is just to see if my SELECT
query finds anything (i.e., rows > 0). In other words, if the SELECT
query finds anything at all.
SELECT COUNT(*)
returns a number to me, like 1, 2 or 3 when I test out the query in MySQL.
However, I am wondering if this number is returned directly to my Perl code through DBI and gets assigned to my $sth
variable.
my $sth = $dbh->prepare("SELECT COUNT(*) FROM `USERS` WHERE FIRSTNAME = ? AND LASTNAME = ?");
$sth->execute($firstName, $lastName);
if ($sth > 0){ #Check if found any matches
print "Found something!";
} else {
print "No matches!";
}
I guess my main question is whether the SELECT COUNT(*)
returns a number and saves directly in my $sth
variable, or if I need to do something else to $sth
to figure out how many rows were found with the query.
Much appreciated.