RE: Make database handle persist in CGI/DBI application?



I have run into this myself. The scope of dbh doesn't exist in your sub
function. I make the my $dbh declaration to an our $dbh. This seems to
do the trick for me.

Chris
-----------------------------------------------
Just Your Friendly Neighborhood
_SPIDEY_


-----Original Message-----
From: Mary Anderson [mailto:mfanderson@xxxxxxxxxxx]
Sent: Thursday, February 23, 2006 12:16 PM
To: dbi-users@xxxxxxxx
Subject: Make database handle persist in CGI/DBI application?


Hi all,

I am writing a Web app in perl/cgi/dbi which (1) attempts
to verify logins by checking against database user passwords,
and (2) follows good dbi practices by logging the user in
once at the beginning of the session instead of logging in
immediately prior to accessing the database.

I am running afoul of CGI parameter passing conventions. I do( and
find) the following

use CGI "standard";
use DBI;

my $dbh;
if ($condition){
$dbh = $DBI:connect($connectionString, $login,
$password); # find connection is successful at this poing

sub fubar{

my $sth=$dbh->prepare($cmd);
# find that $dbh is undefined.

I have tried passing the handle in as a ref parameter, using
$dbhRef = /*dbh (maybe this one was done incorrectly) and
tried passing a $dbhRef as a value on a hidden textfield.
None of these work. I tend to get "not an array" when I try
to do $$dbhref to dereference the handle.

Is there any way to make the handle persist?
Would I have this trouble if I used Mason?

Thanks in advance,
Mary Anderson


-----------------------------------------
The information contained in this message may be privileged,
confidential, and protected from disclosure. If the reader of this
message is not the intended recipient, or any employee or agent
responsible for delivering this message to the intended recipient,
you are hereby notified that any dissemination, distribution, or
copying of this communication is strictly prohibited. If you have
received this communication in error, please notify us immediately
by replying to the message and deleting it from your computer.

Thank you. Paychex, Inc.

.



Relevant Pages

  • Re: globals and modules
    ... sub foo { ... I want to turn many of the Mason components into a module. ... # if I could assign to $dbh in httpd.conf that might work. ...
    (perl.beginners)
  • Re: passing dbh
    ... > dbh when calling a subs. ... Contrary to what the other replies have been, my suggestion is that you just declare your handle with ... If you need it to be avaible to subroutines imported via requireor use, then declare it as ... Additionally, if you do need a copy of the handle to be used exclusively by some sub you can do something like: ...
    (perl.dbi.users)
  • Re: InactiveDestroy with Class::DBI
    ... > needing both parent and children to have access to the database via ... Enabling DBI trace in the client may shed some light on it. ... > the $dbh in a closure in my Class which is inheriting from ... > sub db_Main{ ...
    (perl.dbi.users)
  • Re: problems with require, subs and databasehandles
    ... > I have a main prog and some subs in different files. ... In your sub you are using $dbh as a global, ...
    (perl.beginners)
  • Re: globals and modules
    ... How can I use $dbh ... the database connection instead of using a global variable. ... use DBI; ... sub new { ...
    (perl.beginners)