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

heres the problem. i have to out put a table containing the scores for each team for each sport in which it partakes. the output is fine until i try to output the score when a certain department doesn't compete in the sport. i tried to use the if statement beloe to print a "na" if the team didn't compete but when i do this it ignores the sth5 whileloop and just prints "NA" for all the values i tried to use
if ($sth5->rows == 0){ print "<td> NA </td>; }
but again it just prints out the "NA" for each one. The while loop is used cos the my($thisscore) = $sth5->fetchrowarray() kept kikkin up errors.
#!/usr/local/bin/perl -w use DBI; use CGI qw(:standard); print header; print start_html; $ENV {'ORACLE_HOME'}= '/oracle/u01'; $host="neptune"; $sid="ora1"; $username="tayloja1"; $password="the.best"; $sport = 'basketball'; $dbh = DBI->connect( "dbi:Oracle:host=$host;sid=$sid",$username, $pass +word)|| die "Can't connect to Oracle"; $sth = $dbh->prepare("SELECT year from compsize WHERE sport = \'$spor +t\'")|| die "couldn't prepareSQL statement"; $sth->execute || die "can't execute sql statement"; print "<table border=1> \n"; print "<tr> \n"; print "<th> List of info </th> \n"; print "<tr> \n"; my ($thisyear)= $sth->fetchrow_array(); $sth->finish; print "<tr> \n"; print "<td>$thisyear</td> \n"; print "</tr> \n"; my @deplist = ('Business School', 'Admin/Estates', 'CEAC', 'Civ Eng', +'Combined Honours', 'CSAM', 'EEAP', 'Mod Lang', 'Mech Eng', 'Pharmacy +', 'Vision Sciences'); my @sportlist = ('campus relay', 'archery', 'badminton', 'basketball' +, 'cricket', 'football','hockey','netball','rugby', 'snooker', 'squa +sh','swimming', 'table tennis','tennis','tug of war', 'volleyball' ); print "<table border=1> \n"; print "<tr> \n"; print "<th> List of info </th> \n"; print "<tr> \n"; $sth4 = $dbh->prepare("SELECT department from tempres WHERE year = \'$ +thisyear\' ORDER BY besttwelve DESC ")|| die "couldn't prepare SQL s +tatement"; $sth4->execute || die "can't execute sql statement"; my $thisno = 0; my @newdeplist; while (@row2 = $sth4->fetchrow_array()) { $newdeplist[$thisno] = $row2[0]; $thisno++; } print "<FORM ACTION='/http://www.cs.aston.ac.uk/cgi-bin/cgiwrap/tayloj +a1/deleteseason.pl/' METHOD=/'POST/'>\n"; print "<table border=1> \n"; for $newdep(0..$#newdeplist){ print "<tr>\n"; my $position = $newdep + 1; print "<td>$position</td> \n"; print "<td>$newdeplist[$newdep]</td> \n"; for $sport(0..$#sportlist){ $sth5 = $dbh->prepare("SELECT score from results WHERE year = \'$t +hisyear\' AND sport = \'$sportlist[$sport]\' AND department = \'$newd +eplist[$newdep]\'")|| die "couldn't prepare SQL statement"; $sth5->execute || die "can't execute sql statement"; if ($sth5->rows != 0){ my @row4; while (@row4 = $sth5->fetchrow_array()) { $thisscore = $row4[0]; print "<td>$thisscore</td> \n"; } } elsif ($sth5->rows == 0) { print "<td>NA</td> \n"; } } $sth6 = $dbh->prepare("SELECT total,besttwelve from tempres WHERE year + = \'$thisyear\' AND department = \'$newdeplist[$newdep]\' ")|| die +"couldn't prepare SQL statement"; $sth6->execute || die "can't execute sql statement"; my @row3; while (@row3 = $sth6->fetchrow_array()) { $thistotal = $row3[0]; $twelve = $row3[1]; print "<td>$thistotal</td> \n"; print "<td>$twelve</td> \n"; }
got no ideas on this one guys so any help would b great oh and sorry about the formatting cheers.

Replies are listed 'Best First'.
Re: small cgi output problem
by Malkavian (Friar) on Apr 09, 2001 at 20:41 UTC
    Quick hint, and cause celebre on this site.
    At the top of your code, insert the line:
    use strict;
    And see what it tells you. It may help.

    Cheers,

    Malk
Re: small cgi output problem
by arturo (Vicar) on Apr 09, 2001 at 20:46 UTC

    Using DBD::Oracle, I consistently get $sth->rows returning 0 even when there's data for it. The reason that is, consulting the documentation, is that rows reliably returns the number of rows affected by a statement that *modifies* data, not on SELECT ones:

    For `SELECT' statements, it is generally not possible
    to know how many rows will be returned except by
    fetching them all.  Some drivers will return the
    number of rows the application has fetched so far, but
    others may return -1 until all rows have been fetched.
    So use of the `rows' method or `$DBI::rows' with
    `SELECT' statements is not recommended.
    
    You'll just have to count 'em manually, looks like.

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      Of course, I guess the question here is, how do you know if there were no rows returned... without causing an error... or should you just try to catch the error.
                      - Ant
        Pragmatically speaking, if there are no rows returned, the fetch*() call won't loop very many times. (Assuming 'zero' is akin to 'not very many').

        The practical approach would be to issue a SELECT COUNT(*) query, which makes the database do the counting.

Re: small cgi output problem
by suaveant (Parson) on Apr 09, 2001 at 20:39 UTC
    I don't know if this will help, I don't use Oracle, but a friend of mine does, and I remember him complaining about DBI rows call not working until you did a fetch, so maybe this is why it always printed NA. Only thing I can think is make a query that gives you all the counts. Not much of a solution for the problem, but it may point you in the right direction.
                    - Ant
Re: small cgi output problem
by andye (Curate) on Apr 09, 2001 at 21:35 UTC
    Wotcha,

    The following is just my £0.02...

    • have a look at this article
    • consider using placeholders
    • think about writing a generic function using hashes and join to write your SQL and do your SELECTs
    • and finally... you had the right idea in the first place. You should be able to fetch the row even if there isn't anything in it - then, once you've got it, you can print something sensible if it's empty. Have a fiddle about with it, post the code how it was when it was kikkin' up them errors (grin) - maybe they weren't caused by the database? Or for a different reason?
    Anyway, hope these vague ramblings have been some help,

    andy.