The code above fails to execute the else statement if there is no data in the tableThe body of the while statement will be executed for each row returned by the query. If there are no rows returned, nothing in the while loop will get executed. You need to keep track of whether the while loop found the data you are looking for:
But, why have perl do the searching when you can have the database do it? Just add a WHERE clause to your query:my $found; while (@row = $query->fetchrow_array) { ... if (...found desired data...) { $found = 1; # might also exit loop here with 'last' } } if ($found) { # data was found } else { # data was not found }
my $sql = "SELECT * FROM $course WHERE column = ?"; my $sth = $dbh->prepare($sql); $sth->execute($userid); if (@row = $sth->fetchrow_array) { # row was found where column == $userid } else { # no such row found }
In reply to Re: testing for retuning value of DBI querry is empty
by pc88mxer
in thread testing for retuning value of DBI querry is empty
by demonlazeros
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |