Solved

Retrieving data from a database

Posted on 2001-06-27
4
148 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

828 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