shake7 has asked for the wisdom of the Perl Monks concerning the following question:
Hi everyone, I'm very new to Perl, and I was having a bit of an issue with MySQL and Perl. What I want to do is take the output result and assign it to a Perl variable. Here's what I have so far:
use DBI; my $dsn = 'driver={SQL Server}; Server=SERVERNAMEHERE; database=DBNAMEHERE; uid=UIDHERE; pwd=PWHERE'; my $dbh = DBI->connect("dbi:OOOO:$dsn",{AutoCommit => 1}) or die "$DB +I::errstr\n"; my $sql = "select field1, field2 from location where restriction = 'RE +STRICTION NAME'"; my $sth = $dbh->prepare( $sql ); $sth->execute(); $sth->finish(); $dbh->disconnect();
As you can see, I only show the actual SQL statements, I've looked at countless forum posts to help solve my problem but I don't think they answer my question accurately. For a little more information, the mySQL statement works fine and returns about 100+ results. I want to assign those results to Perl variables, and then pass those variables into another subroutine which utilizes SOAP::Lite If you'd like to take a look, here's the Perl code which I attempted to write thus far, but I only get one result which contains two fields, and not the additional hundred. Would I need to use an array as well?
use DBI; my $dsn = 'driver={SQL Server}; Server=SERVERNAMEHERE; database=DBNAMEHERE; uid=UIDHERE; pwd=PWHERE'; # my %info = ( # field1 => "field1", # field2 => "field2" # ); my $dbh = DBI->connect("dbi:OOOO:$dsn",{AutoCommit => 1}) or die "$DB +I::errstr\n"; #my $ref = $dbh->selectall_arrayref("select field1, field2 from locati +on where restriction = 'RESTRICTION NAME'"); my $sql = "select field1, field2 from location where restriction = 'RE +STRICTION NAME'"; my $sth = $dbh->prepare( $sql ); $sth->execute(); my @record = $sth->fetchrow_array; #my $href = $dbh->selectall_hashref("select field1, field2 from locati +on where restriction = 'RESTRICTION NAME'"); #print "$_\n" for (keys %$href); # foreach (@record){ # print $_; # } print join(", ", @record); $sth->finish(); $dbh->disconnect();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: MySQL results inserted into Perl variable
by boftx (Deacon) on Jul 09, 2014 at 00:01 UTC | |
|
Re: MySQL results inserted into Perl variable
by trippledubs (Deacon) on Jul 09, 2014 at 00:13 UTC | |
by locked_user sundialsvc4 (Abbot) on Jul 09, 2014 at 01:06 UTC | |
by Athanasius (Archbishop) on Jul 09, 2014 at 04:37 UTC | |
by Anonymous Monk on Jul 09, 2014 at 01:29 UTC |