$currentpage = param('currentpage'); &Conn_to_DB; $SQL = "Select * from messages where (username='$username' or toname='$username') and message!='' GROUP BY threadID order by dateadded desc, ID desc"; &DoSQL; $recordcount = $sth->rows; if ($recordcount <= 0) { print "Error message goes here."; } else { # Separately counting the total number of threads is no longer needed (apparently). # $SQL2 = "Select * from messages where (username='$username' or toname='$username') and message!='' order by ID desc"; # &DoSQL2; # %threads; # while ($pointer2 = $sth2->fetchrow_hashref){ # $threads{$pointer2->{'threadID'}}++; # } # $threadcount_all = scalar keys %threads; $titlesperpage = 10 if ($titlesperpage eq ""); $currentpage = 1 if ($currentpage eq "" || $currentpage < 1); $startcount = ($currentpage - 1) * $titlesperpage + 1; $stopcount = $currentpage * $titlesperpage; $current_count = 0; $threadcount=0; $threadIDlist = ""; while (($pointer2 = $sth2->fetchrow_hashref) && ($current_count <= $stopcount)){ $current_count++; if ($current_count >= $startcount && $current_count <= $stopcount) { $name = $pointer2->{'name'}; $message = $pointer2->{'message'}; $date = $pointer2->{'date'}; #this is no longer needed #if ($threadIDlist !~ /\b\Q$threadID\E\b/) { #$threadIDlist=$threadID . "," . $threadIDlist; #$threadcount++; print qq~ Content goes here~; #} end if ($threadIDlist !~ /\b\Q$threadID\E\b/) { } #end if ($current_count >= $startcount && $current_count <= $stopcount) { } #end while (($pointer2 = $sth2->fetchrow_hashref) && ($current_count <= $stopcount)){ sub Conn_to_DB{ use DBI; $DSN = "DBI:mysql:database_name:db.domain.com"; $sqluser = "user"; $sqlpass = "pw"; $dbh = DBI->connect($DSN,$sqluser,$sqlpass) || die "Cannot connect: $DBI::errstr\n" unless $dbh; return; } sub DoSQL{ eval { $sth = $dbh->prepare($SQL); }; # end of eval # check for errors if($@){ $dbh->disconnect; print "Content-type: text/html\n\n"; print "An ERROR occurred! $@\n"; exit; } else { $sth->execute; } # end of if/else return ($sth); } sub DoSQL2{ eval { $sth2 = $dbh->prepare($SQL2); }; # end of eval # check for errors if($@){ $dbh->disconnect; print "Content-type: text/html\n\n"; print "An ERROR occurred! $@\n"; exit; } else { $sth2->execute; } # end of if/else return ($sth2); }