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

Hi Monks
All that I am trying to do here is to only display the values of **$count **, if it has more then 2 items coming from the database otherwise I wanted it to jump out of the while loop and print that one item found. If I do what I have in my code it will not display the first item but it will start from the second I think I know why this is happening but I do not know what is the best way to do it.
Thank you!!!

my $count; while ($pointer = $sth->fetchrow_hashref) { $count++; $type=$pointer->{'NET'}; $car1 =$pointer->{'CAR1#'}; $line =$pointer->{'LINE#'}; print "<br><b>$count</b><br>"; if($count >= 2) { print "<b>**$count **</b> } else { print "<br><b>^^^^ $count_claims ^^^^</b><br>"; } } if($type) { print "<META http-equiv=\"REFRESH\" content=\"0; url=go.htm +\">"; } else { print "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=notfou +nd.html\"> \n"; exit; }

Replies are listed 'Best First'.
Re: Right Number DB
by menolly (Hermit) on Dec 10, 2003 at 20:53 UTC

    If your database supports rows(), and you aren't interested in portability to other databases, you may find that useful.

    my $count = $sth->rows(); if ($count > 1) { # display count } elsif ($count == 1) { # single record stuff } else { # nothing found }

      Careful, though. rows() is only guarenteed to work if you've already fetched all the rows returned. Though you can get away with it on MySQL and maybe some other DBMSes.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

Re: Right Number DB
by ysth (Canon) on Dec 11, 2003 at 01:21 UTC
    You break out of a while loop with break; Can you be more clear about what you want? You say you want to do something if you have more than two, but your code is testing for two or more. If you want to avoid resetting $type, $car1, and $line after the first record, you need to make that conditional:
    while ... ++$count; if ( $count == 1 ) { $type = ... ... }
    or just break out of the while loop if $count > 1 before setting $type, et al.

    If this isn't helpful, please provide what output you expect to see if there are 1, 2, or 3 database records. Your description is unclear.

Re: Right Number DB
by Anonymous Monk on Dec 11, 2003 at 12:56 UTC
    HI there,

    Can someone explain why when I have more than one value in any of my variables and I go in to the if statement I loose the first value and it starts to print from the second value on. Therefore I am not able to say if in any of the results I have more than one value just print all the values but if there is only one I want to use that value for something else like printing it in a url to redirect to another page.

    Thanks for the help!
    my $count=0; while ($pointer = $sth->fetchrow_hashref) { ++$count; $type=$pointer->{'TYPE'}; $number =$pointer->{'NUMBER'}; $line =$pointer->{'LINE'}; if($count > 1) { print "**** $number **** \n"; # If there is more than one v +alue it will start to print from the second one and it will still go +to the ELSE I need to print all the values here and exit from it, jus +t need to know what is the best way to do it. }else { print $number; } } #end of the while

    janitored by ybiC: Reparent into existing thread, format for legibility

      You could do it in two steps -

      Step 1 Explicitly get the row count with SQL -
      SELECT count(*) FROM table where ...
      And then retrieve the rowcount from the query result.

      Step 2 Prepare the actual SQL to fetch the data. Since you already know the row count from the previous step, you can easily implement the required logic.

      $sth = $dbh->prepare($sql); $sth->execute(); if ($rowcount == 1) { # do single row logic # fetch data } else { # do multi-row logic # fetch data with while loop }
      There is a rowcount variable in the DBI, but I would not rely on it because it is not reliable (and database dependent?).

A reply falls below the community's threshold of quality. You may see it by logging in.