Hi,
fetchrow_hashref returns a reference to a hash, with each hash having the
column name as it's keys.
$row = $sth->fetchrow_hashref;
print "$row->{NAME},$row->{ADDRESS}"
or whatever.
Similarly, you could use fetchall_hashref or selectall_hashref. I
understand that the hashref functions are not as efficient as the array ref
functions, so, if performance is an issue, it may be worth performing a
select from the database system tables to determine column names (and
types?) before using the arrayref functions.
There is a table_info function that will give you the table definitions,
but the docs say that this feature is experimental, so use it with caution!
Regards
Phil
-----------------------------------------------------------------------------------------------
Direct: +44 (0) 20 7325 1653 GDP: 325-1653
Mobile: +44 (0) 77 4876 0299 Home: +44 (0) 1444 891365
RTM: 800 625 1275 or + 1(1) 334 420 2916 Code: 435994
------------------------------------------------------------------------------------------------
"Brian McCain"
<bmccain@pagema To: "Manisha Gupta"
<[EMAIL PROTECTED]>, <[EMAIL PROTECTED]>
sters.com> cc:
Subject: Re: retrieving column names
from a table using DBI
18/02/2003 module
23:31
It seems to me that there should be a more direct way to do it, but you
could do:
my $sql = "SELECT * FROM FOO";
my $arrayref = $dbh->selectall_arrayref($sql, { Columns => {} });
my @names;
foreach my $key (keys %{$arrayref[0]}){
push @names, $key;
}
Which would leave you with a reference to an array containing one hashref
per result row and, again, an array containing the names of all your
columns.
Brian McCain
----- Original Message -----
From: "Manisha Gupta" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, February 18, 2003 2:57 PM
Subject: retrieving column names from a table using DBI module
Hello
I am working with Perl CGi. There is a query where i have to select all
columns in a table and display the data on the web page.
the query is something like
"select * from <tablename>
But the problem is I am not able to get the column names.
I am using the DBI module. Is there a way to get column names?
Thanks