LPM: Re: DBI woes

Rich Bowen rbowen at rcbowen.com
Wed Feb 23 21:53:39 CST 2000


David Pitts wrote:
> 
> Rich,
> Here is what you have:
> > $sth = $dbh->prepare("select day from reservation
> > where ID = ?");
> > foreach $ID (@conflicts) {
> > $sth->execute($ID);
> > ($day) = $sth->fetchrow_array;
> > print "$day\n";
> > }
> 
> I would have thought (and not having checked this on the computer), that it
> would have been more like this:
> 
> $sql= <<EOT;
> SELECT day
> FROM reservation
> WHERE ID = ?
> EOT
> $sth = $dbh ->prepare_cached($sql)  or die "\nSQL is $sql\n";
> $sth->execute($ID);;
> while ($sth->fetch){ print "$day\n" }

No, I definately want to loop through an array of IDs, and execute the
statement once for each one. And there's only one day for each ID (ID is
a unique field - autoincrement thingy) so it's not while ($sth->fetch)

The comparable example from the DBI docs is:

$sth = $dbh->prepare("insert into table(foo,bar,baz) values (?,?,?)");
  while(<CSV>) {
    chop;
    my ($foo,$bar,$baz) = split /,/;
        $sth->execute( $foo, $bar, $baz );
  }

But it can also be used for selects.

Rich
-- 
http://www.ApacheUnleashed.com/
Lexington Perl Mongers - http://lexington.pm.org/
PGP Key - http://www.rcbowen.com/pgp.txt



More information about the Lexington-pm mailing list