How do I get multiple rows?

2001-03-15 Thread Scott Phelps


Here's what doesn't work:

my $dbh=DBI-connect('dbi:ODBC:scottp', 'user', 'pass',
{RaiseError = 1,
AutoCommit = 1}
) || errorhandler("Database connection error: $DBI::errstr");

$sth=$dbh-prepare("SELECT $content_field FROM $content_table WHERE page= ?
AND type= ?");
$sth-execute($content_page, $content_type);
@content = $sth-fetchrow_array;
$dbh-disconnect();

This of course returns an array (actually only one element) of the first row
that satisfies my SQL statement.
Is there a way to get the specific field of multiple rows that match my
criteria into an array?

__
splehP ttocS
rotartsinimdA smetsyS TN
secivreS tenretnI renroKbeW
moc.renrokbew@pttocs
moc.renrokbew.www

$a="@ARGV";while($a){$a=~s/(.$)//;$b=$b.$1;}print "$b\n";
__








Re: How do I get multiple rows?

2001-03-15 Thread Chris Winters

* Scott Phelps ([EMAIL PROTECTED]) [010315 10:11]:
 
 Here's what doesn't work:
 
 my $dbh=DBI-connect('dbi:ODBC:scottp', 'user', 'pass',
   {RaiseError = 1,
   AutoCommit = 1}
   ) || errorhandler("Database connection error: $DBI::errstr");
 
 $sth=$dbh-prepare("SELECT $content_field FROM $content_table WHERE page= ?
 AND type= ?");
 $sth-execute($content_page, $content_type);
 @content = $sth-fetchrow_array;
 $dbh-disconnect();
 
 This of course returns an array (actually only one element) of the first row
 that satisfies my SQL statement.
 Is there a way to get the specific field of multiple rows that match my
 criteria into an array?

DBI comes with very good documentation, check it out:

$sth-execute( $content_page, $content_type );
while ( my $row = $sth-fetchrow_arrayref ) {
  ... do stuff with @{ $row }...
}

Chris

-- 
Chris Winters ([EMAIL PROTECTED])
Building enterprise-capable snack solutions since 1988.



Re: How do I get multiple rows?

2001-03-15 Thread Ronald J Kimball

On Thu, Mar 15, 2001 at 09:54:21AM -0500, Scott Phelps wrote:
 
 Here's what doesn't work:
 
 my $dbh=DBI-connect('dbi:ODBC:scottp', 'user', 'pass',
   {RaiseError = 1,
   AutoCommit = 1}
   ) || errorhandler("Database connection error: $DBI::errstr");
 
 $sth=$dbh-prepare("SELECT $content_field FROM $content_table WHERE page= ?
 AND type= ?");
 $sth-execute($content_page, $content_type);
 @content = $sth-fetchrow_array;
 $dbh-disconnect();
 
 This of course returns an array (actually only one element) of the first row
 that satisfies my SQL statement.
 Is there a way to get the specific field of multiple rows that match my
 criteria into an array?

You can fetch all the rows at once, as a reference to an array of array
refs, one array ref per row.  Then, you can flatten the array of arrays
into a plain array.  This works best when you're selecting a single column
from the database.


my $rows = $sth-fetchall_array();
  # ref to array of array refs

@content = map @$_, @$rows;
  # flatten array refs into a single array


Ronald