Undefined value error
- From: yeti349@xxxxxxxxx
- Date: 26 Jul 2005 05:50:19 -0700
Hi, with the following code I can successfully query a datasource and
display the results in html. I have set page links so you can go to the
various pages of the total results. The problem is when you click on a
page link, I'm getting a "Can't call method "prepare" on an undefined
value" at the following line:
my $sth = $dbh->prepare($sql);
This is confusing since the first time through, the handle is
successfully defined and the query works. I've passed the query
parameters back through the url to the subroutine. Why is the handle
undefined the second time through? Thank you.
use DBI;
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
print qq{Content-type: text/html\n\n};
my $office_code = param("office_code");
my $start_date = param("start_date");
my $end_date = param("end_date");
my $reqpage = param('reqpage') || 1;
sarResults($office_code,$start_date,$end_date,$reqpage);
sub sarResults()
{
$office_code = shift;
$start_date = shift;
$end_date = shift;
$reqpage = shift;
my $dbh = DBI->connect('dbi:ODBC:myTable',
{RaiseError => 1, AutoCommit => 1});
#$dbh is defined initially, but undefined when a link is clicked
from a results page
print "\$dbh is ", (defined $dbh) ? "defined" : "undefined"), "\n";
my $sql=qq(SELECT Number,Office,Date_Received
FROM aTable
WHERE Office = '$office_code'
AND Date_Received
BETWEEN '$start_date' and '$end_date');
my $sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement.";
my $pagesize = 10;
my $result_count = 0;
my $pagecount = 0;
while (my @row=$sth->fetchrow_array)
{
if($result_count > (($reqpage - 1) * $pagesize) && $result_count
<= $reqpage * $pagesize)
{
#results displayed here
}
else{print "\n";}
++$result_count;
}
#
# calculate the total number of pages
# to display the search results
#
if ($result_count != 0)
{
$pagecount = int($result_count / $pagesize);
if (($pagecount * $pagesize) != $result_count)
{
$pagecount++;
}
}
#
# get the first and last record for each results page
#
my $firstresult = (($reqpage - 1) * $pagesize) + 1;
my $lastresult = $firstresult + $pagesize - 1;
if ($lastresult > $result_count)
{
$lastresult = $result_count;
}
my $prev_link;
my $next_link;
my $prev_page = $reqpage - 1;
my $next_page = $reqpage + 1;
if ($reqpage == 1)
{
$prev_link = "";
}
else
{
$prev_link = "<a
href=\"http://localhost/myscript.pl?end_date=$end_date&start_date=$start_date&office_code=$office_code&reqpage=$prev_page&pagesize=$pagesize\">"
.. "<< Previous $pagesize Records" . "</a>";
}
if ($reqpage == $pagecount)
{
$next_link = "";
}
else
{
$next_link = "<a
href=\"http://localhost/myscript.pl?end_date=$end_date&start_date=$start_date&office_code=$office_code&reqpage=$next_page&pagesize=$pagesize\">"
.. "Next $pagesize Records >>" . "</a>";
}
my $pagelinks;
my $thislink;
if ($pagecount > 1)
{
$pagelinks = $prev_link;
my $pageno = 0;
while ($pageno < $pagecount)
{
$pageno++;
if ($pageno == $reqpage)
{
$thislink = " <b>$pageno</b> ";
}
else
{
$thislink = " <a
href=\"http://localhost/myscript.pl?end_date=$end_date&start_date=$start_date&office_code=$office_code&reqpage=$pageno&pagesize=$pagesize\">"
.. $pageno . "</a>";
}
$pagelinks = $pagelinks . $thislink;
}
$pagelinks = $pagelinks . " " . $next_link;
}
else
{
$pagelinks = "";
}
print qq(
<br><br><font face=arial size=2>Results <b>$firstresult -
$lastresult</b> of <b>$result_count</b>
records<p><center>$pagelinks<p><center><form name=excel
action=http://localhost/myscript.pl method=post>);
}
.
- Prev by Date: Re: MsSQL DBD::ODBC IsNull and undef
- Next by Date: RE: MsSQL DBD::ODBC IsNull and undef
- Previous by thread: RE: [dbi] Re: MsSQL DBD::ODBC IsNull and undef
- Next by thread: Problem with DBD-Oracle
- Index(es):
Relevant Pages
|
|