Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl/Mysql: Extracting entries from database

How can I get the data in a way which isn't field position specific?

         

Allen

3:58 pm on Jul 1, 2002 (gmt 0)

10+ Year Member



Hi all,

Another Perl/MySQL question for you:

How can I extract records from a database without knowing the positions of the fields, only the field names.

Currently I use:

 $sth = $dbh->prepare ("SELECT * FROM country ORDER BY name ASC");
$sth->execute ();

 while ( ($id, $name) = $sth->fetchrow_array()) {
push @countries, { id => "$id", name => "$name" };
}

 $sth->finish ();
$dbh->disconnect ();

But, as you can see, it requires me to know the order the fields are in.

I did try getting to data into hashes using fetchrow_hashref a while back, but couldn't get it to work.

Thanks in advance,

Allen

Allen

7:42 am on Jul 2, 2002 (gmt 0)

10+ Year Member



Had another look through my books last night and found out why my method for using fetchrow_hashref wasn't working - it was a simple typo :/

In case anyone's wondering, here's how o do a fetchrow_hashref:

while ( $country = $sth->fetchrow_hashref()) {
push @countries, { id => "$country->{id}", name => "$country->{name}" };
}

Allen

Brett_Tabke

2:44 pm on Jul 2, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



That's what drives me nuts about dbi programming. I sure wish I could find a set of free routines that skipped dbi and used pure sockets. I'll handle the escaping and what not...