Results 1 to 2 of 2

Thread: Global declaration for DBI interface and binding stts

  1. #1
    Join Date
    Mar 2003
    Location
    Chennai, India
    Posts
    25

    Global declaration for DBI interface and binding stts

    Hi all,

    I use DBI interface with username and password supplied each time explicitly. But I would like it to be implementation oriented. ie., It should take the username and password assigned initially with a (say) configuration file.

    Could someone shower on me the points that I need to be concerned and the steps by which I can make them. While the sql statements are assigned they should be binded with some function standard in a perl subroutine.

    Thamks in advance.
    Karthikeyan

  2. #2
    Join Date
    Feb 2003
    Location
    Johannesburg, South Africa
    Posts
    145
    There are a number of ways, and here are two popular ones:

    1) A seperate config file:

    Filename example: /etc/myperlconf

    Content:
    Code:
    $dbusername = "root";
    $dbpassword = "";
    
    1;
    To include in perl:

    Code:
    require "/etc/myperlconf";
    2) A seperate perl file containing var declerations as well as common functions:

    Filename example: /var/www/cgi-bin/common.pl

    Content:

    Code:
    sub dbconnect {
    
    	$dbusername = "root";
    	$dbpassword = "";
    	$dbname = "test";
            $db = "dbi:mysql:" . $dbname;
            $dbh1 = DBI->connect( $db, $dbusername, $dbpassword ) or die("DB ERROR : $!");
            $dbh2 = DBI->connect( $db, $dbusername, $dbpassword ) or die("DB ERROR : $!");
    	$dbh3 = DBI->connect( $db, $dbusername, $dbpassword ) or die("DB ERROR : $!");
    
    }
    
    sub decode {
    
            if ( $ENV{'REQUEST_METHOD'} =~ /get/i ) { $buffer = $ENV{'QUERY_STRING'}; }
            else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }
    
            @pairs = split(/&/, $buffer);
            
            foreach $pair (@pairs) {
    	
                    ($name, $value) = split(/=/, $pair);
                    $value =~ tr/+/ /;
                    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
                    $value =~ s/\n/ /g;      # added to strip line breaks 
                    $value =~ s/\r//g;
                    $value =~ s/\cM//g;
                    $FORM{$name} = $value;
    		
            }
    	
    }
    
    1;
    In your perl scripts, use the following:

    Code:
    require "/var/www/cgi-bin/common.pl";
    dbconnect();
    decode();
    You can use the above code in all the Perl scripts that form part of the application.

    Hope that helps

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •