SPUG: while question
Ben Reser
ben at reser.org
Tue Sep 16 13:25:39 CDT 2003
On Tue, Sep 16, 2003 at 10:09:14AM -0700, Peter Darley wrote:
> Folks,
> I have yet another stupid question:
>
> I regularly get data out of a DBI statement handle by doing:
>
> while ($MyData = $sth->fetchrow())
> {
> stuff
> }
>
> This falls down when the data returned from the statement is '0', since
> it's not true, and the while breaks. I've tried while (defined $MyData =
> $sth->fetchrow()), but this is not syntacticly correct, and would break on a
> database NULL (which is undef in perl) anway. I can do something like while
> ($MyRef = $sth->fetchrow_hashref()) {$MyData = $$MyRef{datapoint}; stuff},
> but I'm not too fond of that. Does anyone have a suggestion on a better way
> to construct this loop?
Assuming you are only returning one column you could do this:
$sth->bind_columns(\$MyData);
while ($sth->fetch) {
stuff
}
--
Ben Reser <ben at reser.org>
http://ben.reser.org
"What upsets me is not that you lied to me, but that from now on I can
no longer believe you." -- Nietzsche
More information about the spug-list
mailing list