Undefined value error



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>);

}

.



Relevant Pages

  • Re: A vs B query?
    ... You could convert this to a Crosstab query and include the Charge_Type as ... the Column header, Shift as a Row header, and as a Value ... direct vs. indirect in each of the categories. ... But is there a way to include another row that will display the ...
    (microsoft.public.access.queries)
  • Re: A vs B query?
    ... It would help if you posted the SQL of your query. ... GROUP BY Shift ... direct vs. indirect in each of the categories. ... But is there a way to include another row that will display the ...
    (microsoft.public.access.queries)
  • Re: Monthly Totals Query
    ... The shifts have to come from somewhere, so you would need a table of shifts, and a query that outer-joins this to your existing table. ... If you don't already have one, create a table of the valid shift numbers. ... Turn this into into a crosstab query. ... In the Crosstab row, choose Row Heading. ...
    (microsoft.public.access.queries)
  • Re: DB Result Wizard Error
    ... Also noted that one folder, which contains no information can't be deleted, ... >> For instance Equipment Serial Number, Equipment Model Number, Cost Accounting ... >> to display the new equipment information. ... >> is in the table was added to the query. ...
    (microsoft.public.frontpage.programming)
  • RE: Just starting to design a form (or is it a query?)
    ... A Form is used to display the data and allow ... A query can be the record source for a form. ... that match an ID in the Assessment table. ... Now, assuming this is the case, what you need is a form with a sub form. ...
    (microsoft.public.access.gettingstarted)