On Mon, 22 Sep 2003 07:23:17 +0200 JoeJoeJoeJoe <[EMAIL PROTECTED]> wrote:

> Basically trying to get 1 set of results returned, along with all
> column names at the same time.
> 
> Is there an easier way to do it than this? Here's what I have:

There is, see below.

> (Note: MySQL database)
> 
> my $tablename = "tablename"; #get around taint warning
> $sql = "LISTFIELDS $tablename";
> $sth = $dbh->prepare($sql);
> $sth->execute;
> @generic = @{$sth->{NAME}};
> @perm = @generic;
> $sth->finish;

I am not familiar with MySQL, but if this is working for you that is
fine.  There's no reason to assign to @generic before setting @perm.

> $sql = "Select * from tablename where idnr='$somenumber'";
> $sth = $dbh->prepare($sql);

On the gripping hand, a full list of column names is available at this
point in $sth->{NAME} for any RDBMS.  A common trick for getting all
column names is something like this:

   $dbh -> {RaiseErrors} = 1;  # ALWAYS check for errors.
   my $sth = $dbh -> prepare( "SELECT * FROM table WHERE 0 = 1" );
   my @perm = @{$sth -> {NAME}};

In your case, you already have a perfectly acceptable prepare(), but
you may want to use a placeholder instead of pasting $somenumber into
the SQL.

> $sth->execute;
> @generic = $sth->fetchrow;
> @results = @generic;
> $sth->finish;
> 
> $count = @perm;
> $i = 0;
> do {
> print "$i - $perm[0] is $results[0]<br>";

   print "$i - $perm[$i] is $results[$i]<br>";

> $i++;
> } until ($i >= $count);

   # TMTOWTDI
   print "$_ - $perm[$_] is $results[$_]<br>"
      foreach 0 .. $#perm;

   foreach $i ( 0 .. $#perm ) {
      print "$i - $perm[$i] is $results[$i]<br>";
      # Do some other things too
   }

-- 
Mac :})
** I usually forward private questions to the appropriate mail list. **
Ask Smarter: http://www.catb.org/~esr/faqs/smart-questions.html
Give a hobbit a fish and he eats fish for a day.
Give a hobbit a ring and he eats fish for an age.

Reply via email to