Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!!
I am trying to use a IF statement in my code and if the variable $ag is not true, I want to print the second option as you can see "No ag were found" But instead I am getting a blank SELECT MENU. Why is that happening, any thoughts on that? Thanks a lot!!!
Here is where the problem in the code is.

my $sql = "SELECT DISTINCT name FROM master WHERE name Like \'%$compan +y%\' ORDER BY name;"; if($company){ print "<td colspan=\"4\" height=\"23\" nowrap>&<SELECT NAME=\"compan +y\">"; $sth = $dbh->prepare($sql); $sth->execute() || die $sth->errstr; while ($pointer = $sth->fetchrow_hashref) { $ag = $pointer->{'name'}; $ag_save = $ag; if($ag){ print "<OPTION VALUE=$ag_save>$ag_save</OPTION>"; }else{ print "<OPTION>No Ag were found</OPTION>"; } } print "</SELECT>";

Replies are listed 'Best First'.
Re: If Problem!!!
by dragonchild (Archbishop) on Oct 08, 2003 at 17:09 UTC
    Because your SQL statement isn't returning anything. Since it doesn't, then you never enter your while-loop, thus never entering the if-statement.

    A few comments:

    1. Don't print your HTML out in your Perl script. At the very least, use CGI's methods for creating HTML. Ideally, you'd use templates (a la HTML::Template, Template Toolkit, or Mason, to name a few).
    2. Your SQL can very possibly break, if $company has naughty characters in it. I would use placeholders, if I were you. Something like:
      my $sql = <<__END_SQL__; SELECT DISTINCT name FROM master WHERE name LIKE ? ORDER BY name __END_SQL__ # stuff here $sth->execute("%${company}%"); # more stuff here
    3. You need to error-check your prepare() call as well as your execute() call. prepare() is where the SQL is checked for syntax. execute() is where it is checked against the database. A number of DBD drivers (such as Oracle) will dump out of prepare() if the tablename is wrong, or the like.

    As for your actual problem, I would argue that you haven't through through what you want to do. I would build a list of $ag values within the while-loop. Then, outside the while-loop, I would check to see if you have any $ag values. If you don't, put your "Value not found" select. Otherwise, build the selects.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: If Problem!!!
by huguei (Scribe) on Oct 08, 2003 at 17:03 UTC
    You must test the records returned by the select statement outside the "while" loop:
    $sth->execute() || die $sth->errstr; if ($sth->rows >= 0) { while ($pointer = $sth->fetchrow_hashref) { $ag = $pointer->{'name'}; $ag_save = $ag; if($ag){ print "<OPTION VALUE=$ag_save>$ag_save</OPTION>"; } } } else { print "<OPTION>No Ag were found</OPTION>"; }
    Hugo
Re: If Problem!!!
by dws (Chancellor) on Oct 08, 2003 at 17:06 UTC
    But instead I am getting a blank SELECT MENU. Why is that happening, any thoughts on that?

    If your query returns zero rows (i.e., if fetchrow_hashref() never returns a non-undef value), you'll never make it into your if test, and thus will never emit any <option> tags.

Re: If Problem!!!
by herveus (Prior) on Oct 08, 2003 at 17:04 UTC
    Howdy!

    I'd guess that $ag is " " (or something like that -- a string of blanks). That non-empty value will evaluate to true, but will be - surprise - blank.

    Something like

    $ag =~ s/\s+//; if ($ag) . . .
    ought to have useful effects by stripping out the blanks in $ag before you test it.

    yours,
    Michael
Re: If Problem!!!
by dmitri (Priest) on Oct 08, 2003 at 17:04 UTC
    Looks like a problem with while, not if. Check your SQL query.