Solved

Retrieving data from a database

Posted on 2001-06-27
4
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

635 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