Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I have inherited some code, well actually alot of code that needs to be installed for a new client. The original author seems to be intentionally avoiding helping me out and has provided very little assistance, thus I am turning to you for some guidance.

The code that he is using to connect with DBI is as follows...

sub new { my ($class, %flags) = @_; my ($self) = {}; bless $self, ref($class) || $class; my $server = $flags{'-server'} || $ENV{'DBISERVER'} || die "No ser +ver specified!\n"; my $user = $flags{'-user'} || 'undef'; my $pass = $flags{'-pass'}; $self->{_DB_NORM_HANDLE} = DBI->connect('dbi:' . $server, $user, $ +pass, { RaiseError => 0, AutoCommit => 1 } ) or croak "Cannot connect + to $ENV{'DBISERVER'}\n"; return $self; }
The confusing part is the connect string is defined via a custom apache environment variable which he instructs as follows...
DBISERVER <dbi connect string>
I can handle this within the httpd conf file or even htacess, but what is the correct syntax?

Additionally, the DBI->connect string needs to be able to connect to a specific database on a remote server which is not accounted for in his code.

1) What changes are to be made to the DBI->connect?

2) How do I properly define the custom environment variable

Could someone assist in this matter assuming the following variables...

remote host: somedomain.com
mySQL DB: main_DB
mysql user: charlie
mysql pass: opensesame
TIA for any guidance...

Replies are listed 'Best First'.
Re: DBI question
by Roger (Parson) on Jan 15, 2004 at 06:25 UTC
    The code $ENV{'DBISERVER'} retrieves the DBISERVER environment variable, if the '-server' parameter is not specified by the caller. You could override the default server string when you create the object -
    my $obj = CLASSNAME->new( -server => "mysql:server=xxxx;database=xxxx", -user => "xxxx", -pass => "xxxx" );

    Where CLASSNAME is the name of your class module. I have written a demo below on using mysql via DBI:
    #!/usr/bin/perl -w use strict; use DBI; use DBD::mysql; my $dbh = DBI->connect("dbi:mysql:server=somedomain.com;database=main_ +DB", "USER","PASSWD") or die "Failed to connect to the database!"; my $sth = $dbh->prepare( qq{ SQL } ); $sth->execute(); my @column_headings = @{$sth->{NAME}}; while (my @row = $sth->fetchrow()) { # Data elements $row[0] ... $row[$#row-1] ... } $sth->finish; $dbh->disconnect;