CandymanCID has asked for the wisdom of the Perl Monks concerning the following question:

I will prove my unworthyness once more....

I am querying a SQL db.

I would like the results to be returned and placed into variables such as $Column_Name and $Column_Value.
There is only ONE result that will come from the query as is.

I belevie this can be done with some fetch function, but alas I am not that good yet.

I cower before the wisdom of the monks...

Replies are listed 'Best First'.
Re: Fetch Rover?
by arden (Curate) on Apr 01, 2004 at 20:46 UTC
    CandymanCID, take a look at the database tutorials here at the Monastery first. If you're still in need of help, wander over to CPAN and look at the various DBD and DBI modules. If you still find yourself in need of assistance, come visit us again and provide us with a more specific list of your requirements and, more importantly, what you've accomplished so far.

    - - arden.

      CandymanCID, here's somthing that reports column name. UPDATE: added more context, because original code wasn't clear.
      my @colNames; my $sth1 = $dbh->table_info(); while (my(@tab) = $sth1->fetchrow_array()) { push @colNames, $tab[2]; } open OUT, ">col.txt"; foreach $colName(@colNames) { print OUT "$colName\n"; my $sth = $dbh->column_info(); while (my @info = $sth->fetchrow_array()) { foreach (@info) {$_ = 'NULL' unless defined $_} if ($colName eq $info[2]) { print OUT "\t$info[3]\n";


      The DBI docs on CPAN are good, but dense. My example above should get you going...
Re: Fetch Rover?
by jaco (Pilgrim) on Apr 02, 2004 at 19:28 UTC
    perhaps I'm confused. Are you trying to return the values of the columns into vars with the column names? Or just into vars without having to execute a while loop?
    my ($col1,$col2, $col3) = $dbh->selectrow_array("SELECT col1,col2,col3 + FROM database where whatever='something' limit 1");
    or you could just use a hash
    $sth->execute(); my $names = $sth->{NAME}; while(my $r = $sth->fetchrow_hashref) { foreach my $name (@$names) { print "$name is $r->{$name}\n"; } }