Perl & MySQL question

Mike Cantrell yomahz@devnull.org
Sun, 9 Jul 2000 21:17:50 -0700


Try this... first thing that came to my head:

$blah = [];   <--- no time for creativity

$sql = qq{SELECT col1, col2 FROM table};
$sth = $dbh->prepare($sql);
$sth->execute;
while (my $data = $sth->fetchrow_hashref) {
    push(@{$blah->[0]}, $data->{col1});
    push(@{$blah->[1]}, $data->{col2});
}


BTW, if I remember correctly from the perldoc, fetchrow_hashref is supposed
to a little slow. If you are concerned with performance, try this:

while (my($col1, $col2) = $sth->fetchrow_array) {
    push(@{$blah->[0]}, $col1);
    push(@{$blah->[1]}, $col2);
}


Give that a shot... hope it helps,
Mike Cantrell





----- Original Message -----
From: "Mike Starke" <mgcon@neta.com>
To: <plug-discuss@lists.PLUG.phoenix.az.us>
Sent: Sunday, July 09, 2000 8:09 PM
Subject: Perl & MySQL question


> I have something like the following in a script:
>
> $sql = qq{SELECT col1, col2 FROM table};
> $sth = $dbh->prepare($sql);
> $sth->execute;
> while (my $data = $sth->fetchrow_hashref) {
>    do stuff
> }
> another while loop
>
> I need to take the values from the query and create
> two arrays: @array1 = values in col1, @array2 = values in col2
> after this, I then create a third array
> @array3 = ([@array1], [@array2]);
>
> I have been brute forcing this by making two calls,
> one while loop for col1, and then turn around and another
> while loop for col2. I know the Perl way is there is more than one
> way to do things, but certainlly there is a better way.
> As you can see by the question, I am no Perl expert, but
> anyone willing to lend a hand?
>
> Mike
> mgcon@getnet.com
> http://www.getnet.com/~mgcon
> Phoenix, AZ
> USA
>
>
> _______________________________________________
> Plug-discuss mailing list  -  Plug-discuss@lists.PLUG.phoenix.az.us
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss