Solved

Retrieving data from a database

Posted on 2001-06-27
4
145 Views
Last Modified: 2010-03-05
Hi,

So far I use the code described below to retrieve the data from a database :
   $base->get_result(\%result);
   
   foreach $row (@{$result{data}}) {
      $ligne = "$row->[0],$row->[1],$row->[2],$row->[3],$row->[4],";
      $ligne .= "$row->[5],$row->[6],$row->[7],$row->[8],$row->[9],";
      $ligne .= "$row->[10],$row->[11],$row->[12],$row->[13],$row->[14],";
      $ligne .= "$row->[15],$row->[16],$row->[17],$row->[18],$row->[19],";
      $ligne .= "$row->[20]";
   ...

But now I want to be more flexible and I want to make a loop such as :
   foreach $token ($row) {
    ...

Therefore I don't have to manage explicitly the number of columns of the query.

Thanks in advance
0
Comment
Question by:Richard Coffre
  • 2
4 Comments
 

Expert Comment

by:swonge
ID: 6232009
Hi,
   try following code:
-----------------------------------------------------------
use DBI;
use DBI qw(:sql_types);

sub DBOp {
     my ($dbConn, $query) = (@_);
     $rst = $dbConn->prepare("$query") || &DBErrorRewind($ERRORCODE{"ErrorSQLPrepare"},"getWarning\@parseWarnings.pl",$dbh->errstr(), "$query", $fileName, $Error);    
     $rst->execute() || &DBErrorRewind($ERRORCODE{"ErrorSQLExec"},"getWarning\@parseWarnings.pl",$rst->errstr(), $query, $fileName, $Error);
     return $rst;
}

$query = "select * from db_table";
$rst1 = DBOp($dbConn, $query);

while (@data = $rst1->fetchrow_array()) {
   $val1 = $data[0];
   $val2 = $data[1];
   $val3 = $data[2];
   
   print "val1: $val1 \t val2: $val2 \t val3: $val3\n";
}

----------------------------------------------------------
Hope this help =)


0
 
LVL 3

Accepted Solution

by:
interiot earned 50 total points
ID: 6232030
 $base->get_result(\%result);
 
  foreach $row (@{$result{data}}) {
     my $ligne = '';
     foreach $token (@$row) {
        $ligne .= ", $token";
     }
     print $ligne;
  }


Or, perhaps more succinctly:

  $base->get_result(\%result);

  foreach $row (@{$result{data}}) {
     my $ligne = join(', ', @$row);
     print $ligne;
  }
0
 
LVL 1

Author Comment

by:Richard Coffre
ID: 6234019
The answer didn't follow the programming methodology I use.
0
 
LVL 1

Author Comment

by:Richard Coffre
ID: 6234022
It's exactly what I want easy, simple, short ...

Thanks a lot
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now