Solved

Exiting a While Statement and printing a statement

Posted on 2000-03-01
11
270 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now