Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: DBI forgets last record

by seattlejohn (Deacon)
on Jan 09, 2003 at 07:06 UTC ( #225472=note: print w/replies, xml ) Need Help??

in reply to DBI forgets last record

$sth->fetchrow_array() returns a row (assuming one exists), and has the side effect of moving "past" that row in the set of database results. With this line: print "NO ITEMS IN CART" unless $sth->fetchrow_array();

you try to retrieve a row. There might not be one, in which case you print "no items". But if there is, you don't assign the return value to a variable, so it just gets thrown away -- yet the results pointer still moves forward. That row won't show up in the list you generate in your while loop.

A possible fix could work something like this (untested):

my @fields = $sth->fetchrow_array(); if (!@fields) { print "NO ITEMS IN CART"; } else { while (@fields) { my ($id,$prod_id,$qty) = @fields; print "====== $id ======<br>"; @fields = $sth->fetchrow_array(); } }

        $perlmonks{seattlejohn} = 'John Clyman';

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://225472]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2023-01-28 07:07 GMT
Find Nodes?
    Voting Booth?

    No recent polls found