Solved

print after while loop not executing.

Posted on 1998-11-19
13
310 Views
Last Modified: 2010-03-04
Why is the print statement after this while loop never being reached and how do I fix the problem:

$cursor = $db->prepare($sql) or die "Unable to Execute SQL: $DBI::errstr";
$cursor->execute or die "Unable to Execute SQL: $DBI::errstr" ;
while (@row = $cursor->fetchrow_array or die "<br>FETCH DIED:<br> $DBI::errstr")
{
   print "<Strong>Row $countit</STRONG><br>\n";
   print "@row<br>\n";
   $countit++;
}
print "REPORT COMPLETE<br>";


Thanks
Nina
0
Comment
Question by:ngerda
  • 7
  • 6
13 Comments
 

Author Comment

by:ngerda
ID: 1206371
Adjusted points to 155
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206372
Because when you're out of data, @row comes back as undefined, and thus you die.

fetchrow is listed as being of max speed, with no error checking, etc.  Since you know that the sql executed properly (from the line above the while), just drop that check, and shove out a different message if you have no rows... i.e.

$cursor->execute or die "Unable to execute SQL: $DBI:errstr";
$countit = 0;
while (@row = $cursor->fetchrow_array or die "<br>FETCH DIED:<br> $DBI::errstr")
{
       print "<Strong>Row $countit</STRONG><br>\n";
       print "@row<br>\n";
       $countit++;
}
if ($countit) {
  print "REPORT COMPLETE<br>";
} else {
  print "NO ROWS FOUND<br>";
}
0
 

Author Comment

by:ngerda
ID: 1206373
Now I get this error after the last row is processed in the loop.  How do I fix it so your solution works?

FETCH DIED: at W:\Inetpub\CERPS\cgi-bin\WEBTEST.pl line 36. Database handle destroyed without explicit
disconnect.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 5

Expert Comment

by:b2pi
ID: 1206374
1.) What's at line 36?
2.) I screwed up when I posted that code.... the while line should have been

while(@row = $cursor->fetchrow_array)
{
    ....
0
 

Author Comment

by:ngerda
ID: 1206375
I did that and now get the followING error right before my ClosURE processing below;

ERROR:
dbih_clearcom (h 0xb5e49c, com 0x824e90): FLAGS 0x111: COMSET Warn PrintError TYPE 3 PARENT DBI::db=HASH(0xb5eb94) KIDS 0 (0 active) IMP_DATA undef in 'DBD::Oracle::st' NUM_OF_FIELDS 41 NUM_OF_PARAMS 0

CLOSURE PROCESSING:
print "REPORT COMPLETE<BR>";  *** THIS IS THE LAST LINE TO PRINT OUT
print "</CENTER>\n";
print "</BODY>\n";
print "</HTML>\n";
$db->disconnect() or die "Unable to disconnect : $DBI::errstr";  
$db->Close();
}

SUB ROUTINES FOLLOW.....
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206376
How many new questions can you ask?

You're not giving enough information.... However, looking at your code, what is

$db->Close();

That's not a DBI call... (nor a DBD call)
0
 

Author Comment

by:ngerda
ID: 1206377
The answer I was given just moved the error.  But I'm not heartless .... POINTS INCREASED!  

I took out $db Close and still get the error:

dbih_clearcom (h 0xb5d964, com 0x826560): FLAGS 0x111: COMSET Warn PrintError TYPE 3 PARENT DBI::db=HASH(0x7381b8) KIDS 0 (0 active) IMP_DATA undef in 'DBD::Oracle::st' NUM_OF_FIELDS 41 NUM_OF_PARAMS 0

As I am doing the following closure processing:

print "REPORT COMPLETE<BR>";
print "</CENTER>\n";
print "</BODY>\n";
print "</HTML>\n";
$db->disconnect() or die "Unable to disconnect : $DBI::errstr";  
}

sub getFormData
{
    my($hashRef) = shift;
    my($buffer) = "";   ..... SUBROUTINGES.

Thanks Please Help
(POINTS INCREASED)
Anna
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206378
('just moved the error' indicates that the problem was solved.  Unfortunately, you've got another one)

You're still not saying where you get that error message.

Before you answer, though, put the line

$| = 1;

at the top of your script. Put print statements around places where you think might be causing problems.  Then look at the output.
0
 

Author Comment

by:ngerda
ID: 1206379
Now I get the error:

disconnect(DBI::db=HASH(0xb5a3f4)) invalidates 1 active statement. Either destroy statement handles or call finish on them before disconnecting. at W:\Inetpub\CERPS\cgi-bin\choose02.pl line 96. DBI Handle cleared whilst still active!.
dbih_clearcom (h 0xb5d550, com 0x8274f0): FLAGS 0x111: COMSET Warn PrintError TYPE 3 PARENT DBI::db=HASH(0xb5a3f4) KIDS 0 (0 active) IMP_DATA undef in 'DBD::Oracle::st' NUM_OF_FIELDS 41 NUM_OF_PARAMS 0

$db->disconnect() or die "Unable to disconnect : $DBI::errstr";   IS ON LINE 96.


0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206380
Are you getting all the data you're expecting to get?
0
 

Author Comment

by:ngerda
ID: 1206381
All data comming in fine .. it is just not a message I don't want the client to see.  
POINTS Raised.  Thank You for hanging in here with me.  DO I need to remove cursors or something before I disconnect?

0
 
LVL 5

Accepted Solution

by:
b2pi earned 180 total points
ID: 1206382
No, you need to call finish on the handles....  i.e.

$db->finish;
$db->disconnect;

0
 

Author Comment

by:ngerda
ID: 1206383
You people are good.  Thanks!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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…
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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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