CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
CGI Perl : while loop in CGI perl

 



giridhar276
Novice

Jun 10, 2013, 3:41 AM

Post #1 of 2 (512 views)
CGI Perl : while loop in CGI perl Can't Post

Hi Team,

I am trying to connect to database(succeeded ) and print the records on the browser using while loop. But the elements of array are not displayed instead while loop is displayed directly. Instead of the below I can embed html statements in print but I am looking for the below style as I need to include javascripts and jqueries too.

Could anyone please let me know how to modify the below script(in html tags only) to print the contents of the array.



Code
#!C:\Dwimperl\perl\bin\perl.exe 

# Display script errors.
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use DBI qw(:sql_types);
# Display script errors.
use CGI::Carp qw(fatalsToBrowser);
use HTML::Template;
$q=new CGI;


############ database connectiivty ####################
my ($databaseName, $databaseUser, $databasePw, $dbh);
my ($stmt, $th, @newRow);
$databaseName = "dbi:mysql:tcs";
$databaseUser = "root";
$databasePw = "root123";

# Connect to the database
# Note this connection can be used to
# execute more than one statement
# on any number of tables in the database

$dbh = DBI->connect($databaseName, $databaseUser, $databasePw) || die "Connect failed: $DBI::errstr\n";

$i=0;

$row=();
$sql = "select p_hostname,p_startdate,p_stime from bt_report where p_recovered='-'";

$query2 = "select count(*) from bt_report where p_recovered='-'" ;

$sth = $dbh->prepare($sql) or die "Cannot prepare: " . $dbh->errstr( );

$sth2 = $dbh->prepare($query2);
$sth2->execute();
$sth->execute( ) or die "Cannot execute: " . $sth->errstr( );

$rowcount= $sth2->fetchrow_array();

@final_data=();
while (@row = $sth->fetchrow_array( )) {
$output= join(",", @row);
push(@final_data,$output);
}

$sth->finish( );

# Begin the page.
print "Content-type: text/html\n\n";
print qq~<!doctype html>
<head>
<title>Network Availability Report</title>
while ( $i < $rowcount )
{
print $final_data[$i];

}


</body>
</html>
~;


Tons of thanks...!!!!


BillKSmith
Veteran

Jun 10, 2013, 11:13 AM

Post #2 of 2 (495 views)
Re: [giridhar276] CGI Perl : while loop in CGI perl [In reply to] Can't Post

You really have two problems. The first is pure perl. The other is HTML. I can help you with the perl part.

You are printing a long string which starts with qq~ and ends with the other ~. Your entire while loop is part of this string. Code within a string is never executed. You can fix this problem by replacing the entire loop

Code
while ( $i < $rowcount ) { 
print $final_data[$i];
}

with

Code
@final_data


Your results will now appear in the browser window, but you will not like it. You have to write and output html code to format your results.

I notice that you a using a mixture of cgi.pm and native html. Choose one or the other. I recommend cgi.pm.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives