> > What's the best method for opening one mysql connection and having it > available to all subs in the application? > > I tried using > $self->dbh($dbh); <- Didn't work for me > $self->query('dbh',$dbh) <- Also didn't work for me > > Then I found an example of using > our $dbh= etc. < -- Works, but is this the right way? > > What is the recommended way of doing this? > > > > sub setup { > my $self = shift; > $self->start_mode('mode1'); > $self->mode_param('rm'); > $self->run_modes( > "AUTOLOAD" => 'show_main', > 'mode1' => 'show_main', > ); > > $self->tmpl_path('/usr/local/apache/saint_tmpl/'); > > # Connect to DBI database > > my $database = "saint"; > my $hostname = "localhost"; > my $driver = "mysql"; > my $dsn = "DBI:$driver:database=$database;host=$hostname"; > > our $dbh = DBI->connect($dsn,"user","pass", {'RaiseError'=>1} ) > or die "Couldn't connect to database: " . DBI->errstr; > > # $self->dbh($dbh); > } > > > sub show_main { > my $self = shift; > > # Get CGI query object > my $q = $self->query(); > > my $cookie = $q->cookie(COOKIE); > our ($dbh); # <---------- HERE > > etc. > > }
I use the following: sub setup { my $self = shift; $self->start_mode('out'); $self->mode_param('rm'); $self->run_modes( 'whatever' => \whatever, ); $self->param('dbh' => DBI->connect('<DB CONNECT STRING HERE>')); } sub teardown { my $self = shift; # Disconnect when we're done if ($self->param('dbh')) { $self->param('dbh')->disconnect(); } } sub whatever { my $self = shift; my $q = $self->query(); my $output; $dbh = $self->param('dbh'); ... } Roy [EMAIL PROTECTED] http://www.irubin.com --------------------------------------------------------------------- Web Archive: http://www.mail-archive.com/cgiapp@lists.vm.com/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]