The code above fails to execute the else statement if there is no data in the table
The 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:
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 }
But, why have perl do the searching when you can have the database do it? Just add a WHERE clause to your query:
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.