bjbible
asked on
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</STR ONG></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>";
}
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</STR
}
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>";
}
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>";
}
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>";
}
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
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
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.
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</STR ONG></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>";
}
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
if( @$allrows > 2000 ){
print "YOUR QUERY IS TOO LARGE, USE BATCH PROCESSING SCREEN<BR>";
exit;
} print "<TD><Strong>$Columns</STR
}
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>";
}
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</STR ONG></TD>" ;
}
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
if( @$allrows > 2000 ){
print "YOUR QUERY IS TOO LARGE, USE BATCH PROCESSING SCREEN<BR>";
exit;
}
print "<TR>";
foreach $Columns (@CHDs)
{
print "<TD><Strong>$Columns</STR
}
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.
ASKER
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.
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
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.
if( @$allrows > 2000 ){
print "YOUR QUERY IS TOO LARGE, USE <a href="link.to.batch/screen ">BATCH PROCESSING SCREEN</a><BR>";
$db->disconnect;
exit;
}
print "YOUR QUERY IS TOO LARGE, USE <a href="link.to.batch/screen
$db->disconnect;
exit;
}
ASKER
Here's what I get
**************
DBI::db=HASH(0xe8e6c0)->di sconnect invalidates 1 active statement. Either destroy statement handles or call finish on them before disconnecting. at
**************
DBI::db=HASH(0xe8e6c0)->di
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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</STR
}
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>";
}