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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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



Jun 10, 2013, 3:41 AM

Post #1 of 2 (994 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.


# 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";


$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);
$sth->execute( ) or die "Cannot execute: " . $sth->errstr( );

$rowcount= $sth2->fetchrow_array();

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

$sth->finish( );

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



Tons of thanks...!!!!


Jun 10, 2013, 11:13 AM

Post #2 of 2 (977 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

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



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 and native html. Choose one or the other. I recommend
Good Luck,


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

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