Perl/MySQL/Arrays

W

wombat

I have a question about using arrays in Perl, involving connecting to
MySQL databases.

In the past I've used PHP to access databases and I've been playing
around now with Perl. In PHP to access data in an array you would see
something along the lines of $theresult['firstname'] and this would
retrieve the data under the column "firstname". So far the only way I
see how to retrieve data from an array in Perl would be $theresult['1'],
insert whatever number you want.. hopefully it would be the column for
"firstname". Is there any way around this problem?
 
H

Haitek

my $sql = "SELECT * FROM addresses";
my $sth = $dbh->prepare($sql);
$sth->execute();
while(my $row = $sth->fetchrow_hashref()) {
print "$row->firstname $row->lastname\n";
}
$sth->finish();
 
H

Haitek

Boy, I think I messed that up by combining PHP with Perl. :)

$row->{firstname} $row->{lastname} or $row->{'firstname'} $row->{'lastname'}

lol, I need a break!
my $sql = "SELECT * FROM addresses";
my $sth = $dbh->prepare($sql);
$sth->execute();
while(my $row = $sth->fetchrow_hashref()) {
print "$row->firstname $row->lastname\n";
}
$sth->finish();

I have a question about using arrays in Perl, involving connecting to
MySQL databases.

In the past I've used PHP to access databases and I've been playing
around now with Perl. In PHP to access data in an array you would see
something along the lines of $theresult['firstname'] and this would
retrieve the data under the column "firstname". So far the only way I
see how to retrieve data from an array in Perl would be
$theresult['1'], insert whatever number you want.. hopefully it would
be the column for "firstname". Is there any way around this problem?
 
W

wombat

Haitek <[email protected]> said:
Boy, I think I messed that up by combining PHP with Perl. :)

$row->{firstname} $row->{lastname} or $row->{'firstname'} $row->{'lastname'}

lol, I need a break!
my $sql = "SELECT * FROM addresses";
my $sth = $dbh->prepare($sql);
$sth->execute();
while(my $row = $sth->fetchrow_hashref()) {
print "$row->firstname $row->lastname\n";
}
$sth->finish();

I have a question about using arrays in Perl, involving connecting to
MySQL databases.

In the past I've used PHP to access databases and I've been playing
around now with Perl. In PHP to access data in an array you would see
something along the lines of $theresult['firstname'] and this would
retrieve the data under the column "firstname". So far the only way I
see how to retrieve data from an array in Perl would be
$theresult['1'], insert whatever number you want.. hopefully it would
be the column for "firstname". Is there any way around this problem?

Heh, I actually got locked into trying to figure it out myself and ended
up finding the answer at like 3 in the morning.. YAWN. But thanks. I'm
currently trying to figure out how to count words in a string (or an
array).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top