A couple of valid solutions have been shown, here's another take on the problem (I consider this to be a more natural way of solving it):
my $rows = 0;
while ( defined( my ($id,$prod_id,$qty) = $sth->fetchrow_array()) ){
++$rows;
print "====== $id =====<br>"; # for testing
}
print "NO ITEMS IN CART" unless $rows > 0;
- I like to wrap the entire fetch in a defined() test.
- Since you are already using scalars for each result fetched (as opposed to, say, an array), it might be worth your while to investigate the bind_col method (which provides a good speed boost).
- The $sth->rows() methid is not portable across all databases, notably Sybase. There, the idea is that the only way to see how many rows would be returned by a select is to go and fetch them. Which makes a certain amount of sense. So just use your own counter.
- Remember to call $sth->finish() as the first statement after your while loop.
- I agree with the other monks' assessment in that you are losing the first record, not the last. If you perceive that you are losing the last record in the set, methinks you might be in need of an order by clause to specify the exact order in which you want the results returned.
Hope this helps.
print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'