On Tue, Mar 19, 2002 at 09:21:26PM -0700, Troy Sniff wrote:
> I am trying to perform some column binding by reading the column names
> of a table and binding to variables that coincide with those column
> names.
>
> Example:
>
> -----------
>
> $sth = $dbh->prepare( "SELECT * FROM Table" );
> $sth->execute();
> my $count = 1;
> foreach my $bcs (@{$sth->{NAME}})
> {
> $sth->bind_col( $count, \$$bcs);
> $count++;
> }
>
> -----------
>
> The above works fine, however the following fails.
>
> -----------
>
> $sth = $dbh->prepare( "SELECT * FROM Table" );
> $sth->execute();
> my $BCS = '\$'.join(',\$',@{$sth->{NAME}});
> $sth->bind_columns($BCS);
>
> -----------
>
> Can bind_columns be used in this way?
What is the difference between these two snippets of Perl code?
$x = 1;
$y = 2;
$z = $x + $y;
print "$z\n";
$x = 1;
$y = 2;
$z = '$' . join ' + $', 'x', 'y';
print "$z\n";
To answer your question, although bind_columns could be used in that way,
what you're trying to do is use symbolic references. That's generally a
bad idea. (What would happen if one of the columns were named 'sth'?) Use
a hash instead:
my %values;
$sth->bind_columns(@values{@{$sth->{NAME}}});
or just call fetchrow_hashref().
Ronald