Solved

Retrieving data from a database

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

12 Experts available now in Live!

Get 1:1 Help Now