Solved

Exiting a While Statement and printing a statement

Posted on 2000-03-01
11
288 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

806 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