in reply to testing for retuning value of DBI querry is empty
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 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: testing for retuning value of DBI querry is empty
by demonlazeros (Acolyte) on Apr 22, 2008 at 17:54 UTC |