2

I have written the following code in Perl. I have ActivePerl 5.14 for Windows 7.

#!C:\perl64\bin\perl.exe -wT
use strict;
use warnings;
use DBI;
print "Content-type: text/html \n\n";

# MYSQL CONFIG VARIABLES
my $driver     = "mysql";
my $database   = "test555";
my $tablename3 = "test77";

my $user = "root";
my $pw   = "root";

# PERL MYSQL CONNECT()
my $dbh = DBI->connect("DBI:$driver:$database", $user, $pw,);

my $sth = $dbh->prepare("
        SELECT *
          FROM t6
         WHERE paragraph='PWE1234'
    ");

$sth->execute();
#$dbh->disconnect;
#exit 0;

When the program reaches $dbh->disconnect, the system is throwing an error; hence commented it out. When I comment that out, the system is not throwing any error, but neither do I get output.

There is a result for this query, I checked with MySQL once separately.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
Manikandan
  • 417
  • 4
  • 8
  • 18
  • 3
    You're not printing anything, why do you expect any output? – Mat May 07 '12 at 19:24
  • 1
    "the system is throwing an error"... What error? – gpojd May 07 '12 at 19:42
  • @gpojd, it was throwing an error reg. statement handle was not closed. Since I used Strict, it was throwing up. So, I gave a finish statement for the statement handle, before closing the database handle.Now resolved! – Manikandan May 07 '12 at 20:21
  • @Mannii88, it sounds like you aren't getting all the records before you close the statement handle. That may be by design, but I don't know. I usually `while (my $row = $sth->fetchrow) {...}` to get all the rows and then I don't need to `$sth->finish`. – gpojd May 07 '12 at 20:42
  • @gpojd, can you please give an example with latest module in mind... I am trying parallely with resources from net...I am trying with hashes..I want to print the array...with headers... – Manikandan May 07 '12 at 20:51
  • i am able to print through, while (@row = $sth->fetchrow_array()) { print "@row\n"; but, if i want to use 'for' statement, how to get the row count and give the looping... – Manikandan May 07 '12 at 21:37

1 Answers1

3

There is no output because you have no code to create any output.

After calling execute you need to call one of the fetchsomething methods and do something with the data structure you get back.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • hi @quentin, can you please provide an example or link, where i can find a live example...for fetching array/hashes and printing them in perl.. – Manikandan May 07 '12 at 21:25
  • i am able to print through, while (@row = $sth->fetchrow_array()) { print "@row\n"; but, if i want to use for statement, how to get the row count and give the looping... – Manikandan May 07 '12 at 21:36
  • 1
    I doubt you'll find many (if any) DBDs that tell you the row count of the selected rows until you have actually fetched them all and in the case it hardly matters. You can use fetchall_arrayref/fetchall_hashref to get all the rows in a result-set but obviously you'll need the memory to hold them. You can use MaxRows with those methods to limit the rows returned with each call. There are some good examples in the DBI docs. – bohica May 08 '12 at 07:45
  • `$sth->rows()` would return the number of selected rows. – mpe May 08 '12 at 08:18