2

In Perl I have connected the database using DBI concept. The database connection and select table query is working fine in .pl file. But I have run the DBI database connection code in .pm file. It's not working.

Please review code.

Sample.pl (It's working fine)

use DBI;
my $driver = "mysql"; 
    my $database = "marketplace_perl";
    my $dsn = "DBI:$driver:database=$database";
    my $userid = "root";
    my $password = "root";
    my $dbh = DBI->connect($dsn, $userid, $password );
    my $dbh=connect_db();
        my $sth = $dbh->prepare("SELECT UserEmail,UserPassword FROM pj_user where UserEmail=? and UserPassword=?");

        $sth->execute($UserEmail,$UserPassword);

Marketplace.pm (It's not working)

package Marketplace;
use DBI;
    sub connect_db {
        my $driver = "mysql"; 
        my $database = "marketplace_perl";
        my $dsn = "DBI:$driver:database=$database";
        my $userid = "root";
        my $password = "root";
        my $dbh = DBI->connect($dsn, $userid, $password );
       return $dbh;
    }

    sub login_marketplace { 

    my $dbh=connect_db();
            my $sth = $dbh->prepare("SELECT UserEmail,UserPassword FROM pj_user where UserEmail=? and UserPassword=?");

            $sth->execute($UserEmail,$UserPassword);
            my $User_count=$sth->rows;
    return $User_count

    }

It returns the error message of "Failed to access class (Marketplace): Can\'t locate DBI.pm in @INC (you may need to install the DBI module)"

Please let me know how to fix the DB issue.

simbabque
  • 53,749
  • 8
  • 73
  • 136
Kanewilliam
  • 199
  • 2
  • 18
  • KLooks like DBI is not installed – Jens Aug 31 '18 at 05:41
  • @Jens It's already installed. – Kanewilliam Aug 31 '18 at 05:45
  • @Jens the database connection working in normal perl file. If it's DBI not installed how it's working normal .pl file – Kanewilliam Aug 31 '18 at 05:46
  • Where is the DBI.pm located? – Jens Aug 31 '18 at 05:48
  • /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 – Kanewilliam Aug 31 '18 at 05:53
  • And is that directory in the `@INC` of whatever program is trying to use your module? – Shawn Aug 31 '18 at 05:55
  • @Shawn. No. I have used only use DBI. You mean to use do "File/DBI.pm" method ? – Kanewilliam Aug 31 '18 at 06:04
  • No... Is that directory in the `@INC` of whatever program is trying to use your module? If, say, you're trying to run it with a different version of perl than you were using for your first program, it might not be. – Shawn Aug 31 '18 at 06:11
  • Please provide the FULL error message, and the path to `DBI.pm` (`/usr/local/lib/x86_64-linux-gnu/perl/5.26.1/DBI.pm`?) – ikegami Aug 31 '18 at 07:15
  • @ikegami the full error message is "'Failed to access class (Marketplace): Can\'t locate DBI.pm in atINC (you may need to install the DBI module) (at INC contains: /var/www/kane/CGI-bin) at /var/www/kane/CGI-bin/Marketplace.pm line 3. BEGIN failed--compilation aborted at /var/www/kane/CGI-bin/Marketplace.pm line 3. Compilation failed in require at (eval 104) line 1. " – Kanewilliam Aug 31 '18 at 07:25
  • 2
    Something in your program corrupted `@INC`. – ikegami Aug 31 '18 at 07:27
  • Is this on a shared hosting environment or a virtual server? Does it by any chance run cpanel? – simbabque Aug 31 '18 at 08:32
  • Reopened. The problem isn't that the module isn't installed. That said, there's no information from which to proceed, so voting to close for that reason. – ikegami Aug 31 '18 at 22:17

1 Answers1

1

Try this below code in marketplace.pm file.

use ENV;
my $PERL5LIB= $ENV{'PERL5LIB'};
 package Marketplace;

BEGIN {
 push(@INC, $PERL5LIB);
};
use DBI;
sub connect_db {
my $driver = "mysql"; 
my $database = "marketplace_perl";
my $dsn = "DBI:$driver:database=$database";
my $userid = "root";
my $password = "root";
my $dbh = DBI->connect($dsn, $userid, $password );
return $dbh;
}

sub login_marketplace { 

my $dbh=connect_db();
my $sth = $dbh->prepare("SELECT UserEmail,UserPassword FROM pj_user where UserEmail=? and UserPassword=?");

$sth->execute($UserEmail,$UserPassword);
my $User_count=$sth->rows;
return $User_count

}
VinothRaja
  • 1,405
  • 10
  • 21