• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

Exiting a While Statement and printing a statement

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
bjbible
Asked:
bjbible
  • 8
  • 2
1 Solution
 
RobWMartinCommented:
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
 
RobWMartinCommented:
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
 
maneshrCommented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
RobWMartinCommented:
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
 
RobWMartinCommented:
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
 
RobWMartinCommented:
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
 
RobWMartinCommented:
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
 
bjbibleAuthor Commented:
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
 
RobWMartinCommented:
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
 
bjbibleAuthor Commented:
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
 
RobWMartinCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now