LPM: Re: DBI woes

David Pitts dpitts at mk.net
Wed Feb 23 22:08:20 CST 2000


okay, second thought...

 $sql= <<EOT;
 SELECT day
 FROM reservation
 WHERE ID = ?
 EOT

 $sth = $dbh ->prepare_cached($sql)  or die "\nSQL is $sql\n";
foreach $ID (@conflicts) {
     $sth->execute($ID);
     ($day) = $sth->fetchrow_array;
    print "$day\n";
}



Thanks,

David
David Pitts
President, Professional Consulting Services
www.dpitts.com
dpitts at mk.net
----- Original Message -----
From: Rich Bowen <rbowen at rcbowen.com>
To: <lexington-pm-list at happyfunball.pm.org>
Sent: Wednesday, February 23, 2000 10:53 PM
Subject: Re: LPM: Re: DBI woes


> 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