dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Perl
Expertise: Beginner
Aug 17, 2004

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Connect to the DB for Multiple .cgi Web Services with PERL

Most Web-based Perl applications use more than five .cgi Perl scripts. Instead of typing all the DBI information in the script, I created a function you can use that gets the information from a .dat file on the server, and parses it for the DBI.

It's much more secure, since you're not hand-typing DB login information on the script.

Here is the package script:


################ connectsql.pl #####################################################3
#!c:/perl/bin/perl.exe

package connectsql;

use Exporter;
our @ISA = qw (Exporter);
our @EXPORT = qw (connectsql $config{'dbUser'} $config{'dbServer'} $config{'dbPass'} $config
{'datasource'});


sub connectsql
{
use DBI; 

our $a; 
our $b; 
our $c; 
our $d; 
our %config; 


open FILE, "config.dat" or die "Can not open file.txt $!\n"; 

while (<FILE>) { 
   chomp;   
   ($a,$b,$c,$d) = split(/:/); 
} 

$config{'dbServer'} = $b; 
$config{'dbUser'} = $a; 
$config{'dbPass'} = $d; 
$config{'dbName'} = $c; 
$config{'dataSource'} = "DBI:mysql:$config{'dbName'}:$config{'dbServer'}"; 

our $dbh = DBI->connect($config{'dataSource'},$config{'dbUser'},$config{'dbPass'}) || die"error";
 	
	 
}

Now, all you need to do is add this to your code before the DBI connection:

....
...
....

require "connectsql.pl";
&connectsql::connectsql();
my $dbh = DBI->connect($connectsql::config{'dataSource'},$connectsql::config
{'dbUser'},$connectsql::config{'dbPass'}) || die"error"; 	

....
....
....
The config.dat file is easy:

################config.dat###############
username:host:databasename:password:
Cem Aykan
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date