Dan wrote:
Hi everybody

I've been using DBI for a while now, having made some large programs utilising a mysql db, and all have been successful. But I've come across one project now that may be a little more taxing on my knowledge of DBI. All the while I've been using DBI, I've been getting data by column reference, i.e

@data = $sth->fetchrow_array;
$name = $data[1];
$email = $data[2];
..so on and so forth..

but the project I'm currently writing sees a drastically changing database as it's developed. With writing the web interface in PHP, I don't have to worry about changing the physical layout of the tables, as it retrieves the data by column name. However I'm unable to find any reference to Perl being able to do the same thing? I've looked briefly at hashref, but I don't quite understand it all.

First off, is this even possible? If so, can anyone point me in the right direction as to how to figure it out, or provide me a few lines of sample code I can learn off?

Two ways really. First of all you own code above can be written

  my $data = $sth->fetchrow_hashref;
  my $name = $data->{name};
  my $email = $data->{email};

(as long as those are the correct field names.) I hope that's enough to let you
understand how the fetchrow_hashref call works.

Secondly, you can get a reference to an array of names of the fetched fields
using

  my $fields = $sth->{NAME};

so that you can tie up field names with their position in the array returned by
fetchrow_array. For instance, in the same example, $fields->[1] would be 'name'
and $fields->[2] would be 'email'.

I hope that helps a little.

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to