• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Perl: SELECT from db

Agum

New Member
Hi,

Perl question.
If I want to execute a SELECT query to a SQL database and get its results and basically just print the results out line by line, what is the exact syntax to do it?

From Googling, I basically know so much:

Code:
$sql = "SELECT * FROM table1";

$dbh = DBI->connect( 'DBI:Oracle:instance', 'user', 'pass') or die "dead: ".DBI->errstr;
$sth = $dbh->prepare($sql);
$sth->execute();

while ($sth->fetch())
{
 # i need help here
}

$sth->finish();
$dbh->disconnect;

if there is anything wrong, please correct me.
I mostly need help at what I need to put inside the while loop.
all I need to do is print out the results one by one.
examples online told me to use bind_columns.
I can't really use it because my table has way too many columns and even just picking those that I need, there are way over 20+ of them.

please don't ask me to use PHP, I need to do this in Perl for a reason...

Thanks a lot in advance!
 
I don't mean this as offensive or a questioning of ability;
but is there anybody here now that actually does coding?

I was looking into the threads on this front page and it seems like over 90% are basically questions about existing scripts, where to find this kind of script, installation problems, and so on; even though this is a Programming Help forum.
I remember back in the beginning days of FWS forums there were many actual CGI coders hanging around? are they all gone now?
 
Hello,

You may want to do something like this
PHP:
#prepare the query
my $sth = $dbh->prepare( "
              SELECT *
              FROM people");

#execute the query
$sth->execute( );
## Retrieve the results of a row of data and print
# Fetch each row and print it
while ( my ($field1, $field2, $field3) = $sth->fetchrow_array() ) {
     print STDOUT "Field 1: $field1  Field 2: $field2  Field 3: $field
+3\n";
}

warn "Problem in retrieving results", $sth->errstr( ), "\n"
        if $sth->err( );

exit;
# Disconnect from the database
$sth->disconnect();
 
Last edited:
Back
Top