in reply to Testing to see if a MySQL query is true

A 'query' is not true or false; it returns results (or no results). But you can do your open and initial print's in the first iteration of your while loop. That way if there are no results, you don't print anything. After the loop, you can print any final line(s) and close the pipe if there were any results.
  • Comment on Re: Testing to see if a MySQL query is true

Replies are listed 'Best First'.
(tye)Re: Testing to see if a MySQL query is true
by tye (Sage) on Oct 22, 2002 at 22:59 UTC

    The phrase "in the first iteration of your while loop" made me think of something other than the way I would do it, but the idea is at least very similar. Like this:

    $sth->execute(); my @ary= $sth->fetchrow_array(); if( @ary ) { open(MAIL,"|$mailprog -t"); print MAIL ... do { print MAIL join ("\t", @ary), "\n"; } while( @ary = $sth->fetchrow_array() ); print MAIL "\nEND OF SCHEDULED EVENTS!\n"; close(MAIL); } $sth->finish();

            - tye (at least very similar to "Tye")
      I'm going to go out on a limb and assume akm2 doesn't have a million appointments a day, in which case we may as well use selectall_arrayref:
      my $results = $dbh->selectall_arrayref($sql_statement); if (@$results) { open... print... print ... join("\t", @$_),"\n" for @$results; print... close... }