develooper Front page | perl.dbi.users | Postings from March 2005

DBI->Connect returning UNDEF in cgi but not in standalone prog

Thread Next
From:
Sunil A.V.
Date:
March 22, 2005 09:29
Subject:
DBI->Connect returning UNDEF in cgi but not in standalone prog
Message ID:
20050322173029.31874.qmail@webmail50.rediffmail.com
Hi,

I am trying to connect to database for my web application based on perl cgi and getting problems.

I have a utility function for the purpose. The wierd thing is when I call the utility function from my cgi program, it is returning UNDEF and so not working. If I call the same function from another standalone perl program, the function is retuning the proper db handle through which I am able to connect to db and do db stuff.
Anything I am missing? Should the web based way of calling should be any different?

Standalone prog results: prints the count of table properly
index.cgi gives "Can't call method "prepare" on an undefined value"

Note : programs below

Thanks,
Sunil



Call_New.pl [standalone- working]
===========
use myapp_utility;
print authorized_user('myuserid');



index.cgi [First page of my web application- not working]
==========================================================
use lib ("/opt/iplanet/cgi-bin/myapp");  
unshift(@INC, ".","/opt/iplanet/cgi-bin/myapp");
use CGI qw(:standard :html3);
use CGI::Carp qw(fatalsToBrowser);
use myapp_utility;
if(authorized_user('myuserid')) {
	dispay_page();
} else {
	display_error_page();
}



myapp_utility.pm [utility package]
================================
use CGI qw(:standard :html3);
use DBD::Oracle qw(:ora_types);
use DBI;
$^W = 1; # all objects *must* be -w clean

require Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);

$VERSION = 0.01;

# inheritance heirarchy
@ISA = qw(Exporter);

@EXPORT = qw
(
   authorized_user
)
use vars qw
(
 &authorized_user
)

###############################################################################
# NAME OF THE SUB-ROUTINE: db_connect
# INPUT PARAMETERS       : None
# FUNCTION               : Creates a connection with the database and returns
#                          the database handle
###############################################################################
sub db_connect
{
$g_myapp_database_schema = 'myapp';

$ENV{'ORACLE_HOME'}  = '/opt/oracle/product/9.2.0.6.0_64c';
$G_db_connect_string = 'dbi:Oracle:ledmyapp';
$G_db_user           = 'myapp';
$G_db_passwd         = 'xxxxx';
   my $dsn  = shift || ($G_db_connect_string ? $G_db_connect_string : undef);
   my $user = shift || ($G_db_user           ? $G_db_user           : undef);
   my $pass = shift || ($G_db_passwd         ? $G_db_passwd         : undef);
   my $db_handle;
   $db_handle = DBI->connect($dsn, $user, $pass, {AutoCommit => 0}) || undef;
   return $db_handle;
}

###############################################################################
# NAME OF THE SUB-ROUTINE: db_disconnect
# INPUT PARAMETERS       : $db_handle
# OUTPUT PARAMETERS      : 1/0
# FUNCTION               : Receives a database handle and closes the database
#                          connection
###############################################################################
sub db_disconnect
{
$g_myapp_database_schema = 'myapp';

$ENV{'ORACLE_HOME'}  = '/opt/oracle/product/9.2.0.6.0_64c';
$G_db_connect_string = 'dbi:Oracle:ledmyapp';
$G_db_user           = 'myapp';
$G_db_passwd         = 'xxxxx';
   my $db_handle = shift;
   my $rc;
   if(defined $db_handle)
   {
      $rc = $db_handle->disconnect;
   }

   return $rc;
}

###############################################################################
# NAME OF THE SUB-ROUTINE: authorized_user
# INPUT PARAMETERS       : $user
# OUTPUT PARAMETERS      : 1/0
# FUNCTION               : Authenticates user in the myapp database
#                          Returns 1 if user is authenticated
#                          Returns 0 if user is invalid
###############################################################################
sub authorized_user
{

$ENV{'ORACLE_HOME'}  = '/opt/oracle/product/9.2.0.6.0_64c';
$G_db_connect_string = 'dbi:Oracle:ledmyapp';
$G_db_user           = 'myapp';
$G_db_passwd         = 'xxxxx';
   my $user = shift;
   my $g = db_connect();
   my $sql = qq
      {
         SELECT
            COUNT(*)
         FROM
            myapp_USER
      };
   my $sth = $g->prepare($sql);
   unless ($sth)
   {
      $main::G_database_error = 1;
      return 0;
   }
   unless ($sth->execute())
   {
      $main::G_database_error = 1;
      return 0;
   }
   my $user_authorization;
   $sth->bind_columns(\$user_authorization);
   $sth->fetch();
   $sth->finish();
   if(defined $g) {
           $g->disconnect();
   }
   return $user_authorization;
}

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About