Click to See Complete Forum and Search --> : Global var being dumped/flushed..
chuckdawit
06-11-2007, 01:15 PM
I have a Perl script using the CGI and DBI modules. I've created a username and password page to login. These values get passed to my script where they are checked and used to login into my db. My database handle ($dbh) is saved as a global variable so I can call it later, which I do. The problem I'm having is when I go to another page after I perform my db search I pass in the database handle to connect to the database but when I do this the value is null. It seems like my global variable is being dumped after I call a submit in one of my html forms.
Does anyone know why this is happening? I can send code if needed?
-Chuck
chuckdawit
06-11-2007, 01:25 PM
Here is partial code. The second part of the if..else statement connect to the db and stores the dbh into a global. Then it calls displayQueryScreen to allow the user to enter a search. When the user enters a search it call displayQueryResults but the global var $dbh gets dumped and doesn't have a value.
if ($CGI->param("submit_query")) {
displayQueryResults($CGI, $DBH);
}
elsif ($CGI->param("connect2DB") and $CGI->param('username') ne "" || $CGI->param('password') ne "") {
#CONNECT TO DB
$DBH = DBI->connect("dbi:Oracle:XE", "hr", $CGI->param('password'))
or die "Cannot connect" . $DBI::errstr;
displayQueryScreen($CGI, $OPTION);
}
Nedals
06-11-2007, 10:11 PM
If submit_query is true, $dbh never gets set.
Remember that a CGI script is stateless. So $dbh needs to be set each time the script is run.
use strict; will also help to avoid these type of problems...
#CONNECT TO DB
my $DBH = DBI->connect("dbi:Oracle:XE", "hr", $CGI->param('password'))
or die "Cannot connect" . $DBI::errstr;
if ($CGI->param("submit_query")) {
displayQueryResults($CGI, $DBH);
}
elsif ($CGI->param("connect2DB") and $CGI->param('username') ne "" || $CGI->param('password') ne "") {
displayQueryScreen($CGI, $OPTION);
}