Solved

Exiting a While Statement and printing a statement

Posted on 2000-03-01
11
304 Views
Last Modified: 2010-03-05
Part of my code is below.  

In addition to the "NO DATA FOUND" or "REPORT COMPLETE" messages, I want to add something that counts the records and IF the count is equal to or great than 2000  the query stops processing and prints out a message
"YOUR QUERY IS TOO LARGE, USE BATCH PROCESSING SCREEN".

Using my code below, what would you add to it and where?
************************************************************
print "<TR><TD COLSPAN=10><STRONG>Report Request </STRONG> $AllStmnt</TD></TD>\n";
   $cursor = $db->prepare($sql) or die "Unable to Execute SQL: $DBI::errstr";
   $cursor->execute or die "Unable to Execute SQL: $DBI::errstr" ;
   ##print "$sql";
   print "<TR>";
   foreach $Columns (@CHDs)
   {
    print "<TD><Strong>$Columns</STRONG></TD>";
   }
   print "</TR>";
   while (@row = $cursor->fetchrow_array)
   {
      print "<TR>";
      foreach $Fields (@row)
      {
         print "<TD ALIGN=RIGHT>$Fields</TD>";
      }
      print "</TR>";
      $countit++;
   }
   print "</TABLE>";
   }  
   if ($TotalFound eq 0)
   {
   print "NO DATA FOUND<BR>";
   }
   else
   {
   print "REPORT COMPLETE<BR>";
}

0
Comment
Question by:bjbible
  • 8
  • 2
11 Comments
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2574088
Added a last unless ... as the first statement of your row reading while loop.  "last" says exit the loop.

Since some context is missing from the script, I don't understand the $TotalFound test.  I just used the $countit variable for the error message  conditional.

BTW:  if $TotalFound is a number you should use = instead of eq.

print "<TR><TD COLSPAN=10><STRONG>Report Request </STRONG> $AllStmnt</TD></TD>\n";
   $cursor = $db->prepare($sql) or die "Unable to Execute SQL: $DBI::errstr";
   $cursor->execute or die "Unable to Execute SQL: $DBI::errstr" ;
   ##print "$sql";
   print "<TR>";
   foreach $Columns (@CHDs)
   {
    print "<TD><Strong>$Columns</STRONG></TD>";
   }
   print "</TR>";
   while (@row = $cursor->fetchrow_array)
   {
      last unless $countit < 2000;
      print "<TR>";
      foreach $Fields (@row)
      {
         print "<TD ALIGN=RIGHT>$Fields</TD>";
      }
      print "</TR>";
      $countit++;
   }
   print "</TABLE>";
   }

   if ($countit >= 2000){
      print "YOUR QUERY IS TOO LARGE, USE BATCH PROCESSING SCREEN<BR>";  
   }
   if ($TotalFound eq 0)
   {
   print "NO DATA FOUND<BR>";
   }
   else
   {
   print "REPORT COMPLETE<BR>";
}
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2574104
I forgot the else:

if ($countit >= 2000){
      print "YOUR QUERY IS TOO LARGE, USE BATCH PROCESSING SCREEN<BR>";    
   } else if ($TotalFound eq 0)
   {
   print "NO DATA FOUND<BR>";
   }
   else
   {
   print "REPORT COMPLETE<BR>";
}
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2574115
you could do this in more than one way.

lets say your sql statement is ...

$sql="select field1, field2 from table where this=that";


you can make a similar statement but replace the fields with count (*).
i.e.

$sql="select count(*) from table where this=that";

You put this statement before the actual SQL statement.
then you use....

$cursor = $db->prepare($sql) or die "Unable to Execute SQL: $DBI::errstr";
$cursor->execute or die "Unable to Execute SQL: $DBI::errstr" ;

while (($no_of_records) = $cursor->fetchrow_array) {
 if ($no_of_records>=2000){
  print "YOUR QUERY IS TOO LARGE, USE BATCH PROCESSING SCREEN";
  exit; ## or maybe redirect the user to some other page
 }
}

$sql="select field1, field2 from table where this=that";

and all your normal processing.

====================================================

the second method is to modify your existing loop as follows....

while (@row = $cursor->fetchrow_array) {
push (@display,"<TR>");
                         foreach $Fields (@row)
                         {
push (@display,"<TD ALIGN=RIGHT>$Fields</TD>");
                         }
push (@display,"</TR>");
                         $countit++;
                      }

if ( $countit >2000){
  print "YOUR QUERY IS TOO LARGE, USE BATCH PROCESSING SCREEN";
  exit; ## or maybe redirect the user to some other page
}else{
  foreach(@display){
   print $_,"\n";
  }
}


What you are doing here is not displaying the results in the first loop (as you are doing now), but store it into an array. Now this array is displayed ONLY if the count of records in less than 2000
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:RobWMartin
ID: 2574191
Good idea on waiting to display till after the count is known.  You should still watch $countit in the loop and exit if it exceeds 2000.  Otherwise, @display could grow too large.
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2574252
A variation on the @display theme, is to use the SELECT feature which automatically limits the number of rows returned.  E.g.:

select * from table limit 2001

then you can grab all of them into an array with

$cursor->fetchall_arrayref;

So,


print "<TR><TD COLSPAN=10><STRONG>Report Request </STRONG> $AllStmnt</TD></TD>\n";
   $cursor = $db->prepare($sql) or die "Unable to Execute SQL: $DBI::errstr";
   $cursor->execute or die "Unable to Execute SQL: $DBI::errstr" ;
   ##print "$sql";
   print "<TR>";
   foreach $Columns (@CHDs)
   {

$allrows=$cursor->fetchall_arrayref;
if( @$allrows > 2000 ){
  print "YOUR QUERY IS TOO LARGE, USE BATCH PROCESSING SCREEN<BR>";    
   exit;
}   print "<TD><Strong>$Columns</STRONG></TD>";
   }
   print "</TR>";
   for $row (@$allrows)
   {
      print "<TR>";
      foreach $Fields (@$row)
      {
         print "<TD ALIGN=RIGHT>$Fields</TD>";
      }
      print "</TR>";
      $countit++;
   }
   print "</TABLE>";
   }    
   if ($TotalFound eq 0)
   {
   print "NO DATA FOUND<BR>";
   }
   else
   {
   print "REPORT COMPLETE<BR>";
}


0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2574269
Well, that sure got hosed up.  This little input box gets me every time.  The top portion of the code should actually be:

print "<TR><TD COLSPAN=10><STRONG>Report Request </STRONG> $AllStmnt</TD></TD>\n";
   $cursor = $db->prepare($sql) or die "Unable to Execute SQL: $DBI::errstr";
   $cursor->execute or die "Unable to Execute SQL: $DBI::errstr" ;
   ##print "$sql";

$allrows=$cursor->fetchall_arrayref;
if( @$allrows > 2000 ){
  print "YOUR QUERY IS TOO LARGE, USE BATCH PROCESSING SCREEN<BR>";      
   exit;
}  
   print "<TR>";
   foreach $Columns (@CHDs)
   {
print "<TD><Strong>$Columns</STRONG></TD>";
   }
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2574281
I should reiterate.  The above script should only be used if $sql contains a select statement with a LIMIT clause.  Otherwise, the array of arrays ($allrows) could get too big.
0
 

Author Comment

by:bjbible
ID: 2576438
RobWMartin

I added the 'limit 2001' in the $sql statment.
And replaced my code with your sample...

print "<TR><TD COLSPAN=10><STRONG>Report Request </STRONG>
    $AllStmnt</TD></TD>\n";
    $cursor = $db->prepare($sql) or die "Unable to Execute SQL: $DBI::errstr";

    $cursor->execute or die "Unable to Execute SQL: $DBI::errstr" ;

     $allrows=$cursor->fetchall_arrayref;
      if( @$allrows > 2000 ){
      print "YOUR QUERY IS TOO LARGE, USE BATCH PROCESSING  SCREEN<BR>";      
      exit;
      }    
      ........
                      }
***************************************
It works well - When the query is too large, the "too large" message is displayed  and also "
Database handle destroyed without explicit disconnect".  I want to give the user options to go back and narrow the scope of their query or go to the batch process screen - but NOT show the database handle destroyed message.

POINTS INCREASED TO 100.
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2577174
if( @$allrows > 2000 ){
      print "YOUR QUERY IS TOO LARGE, USE <a href="link.to.batch/screen">BATCH PROCESSING  SCREEN</a><BR>";
      $db->disconnect;
      exit;
}  

 

 
     
0
 

Author Comment

by:bjbible
ID: 2577399
Here's what I get

**************
DBI::db=HASH(0xe8e6c0)->disconnect invalidates 1 active statement. Either destroy statement handles or call finish on them before disconnecting. at
0
 
LVL 3

Accepted Solution

by:
RobWMartin earned 100 total points
ID: 2577720
if( @$allrows > 2000 ){
      print "YOUR QUERY IS TOO LARGE, USE <a href="link.to.batch/screen">BATCH PROCESSING  SCREEN</a><BR>";
      $cursor->finish;
      $db->disconnect;
      exit;
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Title # Comments Views Activity
Perl efficient DB Call 8 96
Rename file based on contents in the file? 3 101
Replace  text in a file 2 122
XPath Syntax help in conjunction with perl XML::LibXML 18 129
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

756 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