Agreed. I tried to stick as close to the original poster's code. Recently I much 
prefer calling fetchrow_hashref and having everything in that one little bundle, but I 
felt that might be a big jump to make...

And of course we could always go down the road of 'selectcol_arrayref' since he was 
only looking for one column in the select and then using it as a list.... Boy I need 
to get back into DBI programming I do miss it in my tedious databaseless world at work.

http://danconia.org 


------------------------------------------------
On Wed, 27 Nov 2002 09:36:55 -0500, Hardy Merrill <[EMAIL PROTECTED]> wrote:

> Comments below.
> 
> [EMAIL PROTECTED] [[EMAIL PROTECTED]] wrote:
> > See inline.
> > 
> > 
> > ------------------------------------------------
> > On Wed, 27 Nov 2002 05:02:16 -0800, "Ramon Hildreth" <[EMAIL PROTECTED]> wrote:
> > 
> > > Hi, I am not getting any output using the below code.
> > > 
> > > #!/usr/bin/perl
> > > 
> > > use strict;
> > > use warnings;
> > > use DBI;
> > > --
> > > connecting to database (okay with this, it works)
> > > 
> > > --
> > > my (@centers, $stmt, $sth, $row);
> > > $stmt = qq { SELECT group_center FROM places};
> > > $sth = $dbh->prepare($stmt);
> > > $sth->execute();
> > > 
> > > #here is the problem area
> > > while ($row = $sth->fetchrow_array()) {
> > 
> > Here you are storing your fetchrow_array to a scalar, which means $row contains 
>the number of columns in the select (1), you need to store it to an array:
> > 
> > while (@row = $sth->fetchrow_array()) {
> > 
> > > push @centers, $_;
> 
> [EMAIL PROTECTED] is right, but I prefer to handle fetchrow_array
> in a slightly different way - one that IMHO is clearer and more
> straightforward.  Rather than receiving fetchrow_array into something
> like array @row where you need to use numeric array subscripts to
> access column values, instead receive fetchrow_array into a list of
> columns that you selected, like this:
> 
>   $stmt = qq{ SELECT col1, col2, col3 FROM some_table };
>   ... prepare ... execute ...
>   while(($col1, $col2, $col3) = $sth->fetchrow_array()) {
>      print "col1=$col1, col2=$col2, col3=$col3\n";
>   }
> 
> Using this method, you need the parenthesis around ($col1, $col2, $col3)
> to ensure "list" context - you need this since fetchrow_array
> actually gives you back an array - you need to receive that into
> an array or a list (or a scalar if all you care about is
> *how many* elements are in the array).
> 
> So, in your case, that would look like this:
>   $stmt = qq{ SELECT group_center FROM some_table };
>   ... prepare ... execute ...
>   while(($group_center) = $sth->fetchrow_array()) {
>      print "Group Center = $group_center\n";
>   }
> 
> Another technique is using "bind columns" - read about that in
> the fine DBI perldocs by doing "perldoc DBI" at a command
> prompt.
> 
> HTH.
> -- 
> Hardy Merrill
> Senior Software Engineer
> Red Hat, Inc.
> 
> > 
> > What is $_ set to here?  You really want to push $row[0] or even the whole array 
>(@row) as you only have one element.
> > 
> > > }
> > > foreach (@centers) {
> > > print "$_\n";
> > > }
> > > I get a blank line for each line, where I would expect to see names of
> > > groups from my query. ???
> > > 
> > 
> > http://danconia.org
> 

Reply via email to