LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Can't locate object method "connect" via package "mysql" (perhaps you forgot to load (https://www.linuxquestions.org/questions/linux-newbie-8/cant-locate-object-method-connect-via-package-mysql-perhaps-you-forgot-to-load-4175413734/)

xman fiji 06-27-2012 06:21 PM

Can't locate object method "connect" via package "mysql" (perhaps you forgot to load
 
i m getting this message when i m trying to run my perl script...please can any one hellp me....

* i have already set cpan
* and installed mysql.pm
* installed DBI.pm


Can't locate object method "connect" via package "mysql" (perhaps you forgot to load "mysql"?) at ./scriptfTesting.cgi line 46.

rigor 06-27-2012 07:22 PM

If I use code with this pattern:

Code:

use DBI ;

$dsn = "DBI:mysql:database=$database;host=$hostname;port=$port" ;

$dbh = DBI->connect($dsn, $user, $password) ;

of course with the generalities replaced with specifics, but then comment out the "use" line,
I get the message:

Quote:

Can't locate object method "connect" via package "DBI" (perhaps you forgot to load "DBI"?) at mysql.pl line 5.
so maybe you left out a "use"?

xman fiji 06-27-2012 08:07 PM

Now i m getting this problam

root@Ubuntu-Server:/var/www/corporate/cgi-bin# ./scriptfTesting.cgi
Name "main::hostname" used only once: possible typo at ./scriptfTesting.cgi line 50.
Name "main::password" used only once: possible typo at ./scriptfTesting.cgi line 54.
Name "main::pw" used only once: possible typo at ./scriptfTesting.cgi line 41.
Name "main::port" used only once: possible typo at ./scriptfTesting.cgi line 50.
Name "main::connect" used only once: possible typo at ./scriptfTesting.cgi line 58.
Name "main::host" used only once: possible typo at ./scriptfTesting.cgi line 40.
Name "main::dbh" used only once: possible typo at ./scriptfTesting.cgi line 54.
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Thu Jun 28 00:56:15 2012 GMT
Cache-Control: no-cache, must-revalidate
Pragma: no-cache
Content-type: text/html


<html>
<head>
<title>Corporate User</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#0099ff">

Use of uninitialized value $hostname in concatenation (.) or string at ./scriptfTesting.cgi line 50.
Use of uninitialized value $port in concatenation (.) or string at ./scriptfTesting.cgi line 50.
Can't locate object method "connect" via package "DBI" (perhaps you forgot to load "DBI"?) at ./scriptfTesting.cgi line 54.
root@Ubuntu-Server:/var/www/corporate/cgi-bin#
____________________________________________________________________________________________________ _____________________

This is perl script

#! /usr/bin/perl -w


#use strict;
#use DBI;
#use Mysql::Statement;
#use strict;
#use warnings;
use DBI ;
#my @ary = DBI->available_drivers();
#print join("\n", @ary), "\n";

#use DBI;
#use Mysql;

# Print out the web page
print "Expires: Mon, 26 Jul 1997 05:00:00 GMT\n"; # Date in the past
print "Last-Modified: " . scalar gmtime() . " GMT\n"; # always modified
print "Cache-Control: no-cache, must-revalidate\n"; # HTTP/1.1
print "Pragma: no-cache\n"; # HTTP/1.0
print "Content-type: text/html\n\n";
#print "'$FORM{EngineerName}'";

print <<EOHTML;

<html>
<head>
<title>Corporate User</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#0099ff">

EOHTML

if (1) {
#####################################################
# Set up variables
$database = "mydb"; # database to connect to
$user = "root";
$host = "127.0.0.1"; # host
$pw = "XXXXXX";
#####################################################
}

# PERL MYSQL CONNECT()
###$connect = mysql->connect($host, $database, $user, $pw);
###my $dbh =mysql->connect($host, $database, $user, $pw);


$dsn = "DBI:mysql:database=$database;host=$hostname;port=$port" ;

# SELECT DB
###$connect->selectdb($database);
$dbh = DBI->connect($dsn, $user, $password) ;

$myquery = "select Date,CompanyName,IPAddress,SubnetMask,EngineerName from Corporate"; #where EngineerName= '$FORM{EngineerName}' order by Date";

$execute = $connect->query($myquery);


print <<EOHTML;

<hr>
<br><font face="Tahoma" size="3"><b><u>Check Corporate User Information</u></b></font></br><hr>
<table border=1 bgcolor="#ADFF2F" cellpadding=5 width=90%>
<tr>
<th>Date</th><th>CompanyName</th><th>IPAddress</th><th>SubnetMask</th><th>EngineerName</th>
</tr>

EOHTML

while (@data = $execute->fetchrow_array()) {
print ("<tr>\n");
print ("<td><font face=tahoma size=2>".($data[0])."</font></td>\n");
print ("<td><font face=tahoma size=2>".($data[1])."</font></td>\n");
print ("<td><font face=tahoma size=2>".($data[2])."</font></td>\n");
print ("<td><font face=tahoma size=2>".($data[3])."</font></td>\n");
print ("<td><font face=tahoma size=2>".($data[4])."</font></td>\n");
print ("</tr>\n");
}

print <<EOHTML;
</table>
<hr>
<br>
</font>
</td>
</tr>

<tr><td valign=top>

</font>
</td>
</tr>
</tr>
<tr><td valign=top>
<font>
</font>
</td>
</tr>
</table>
<hr>
</center>
EOHTML

print <<EOHTML;

</body>
</html>

EOHTML
____________________________________________________________________________________________________ _______________________________



Quote:

Originally Posted by kakaka (Post 4713685)
If I use code with this pattern:

Code:

use DBI ;

$dsn = "DBI:mysql:database=$database;host=$hostname;port=$port" ;

$dbh = DBI->connect($dsn, $user, $password) ;

of course with the generalities replaced with specifics, but then comment out the "use" line,
I get the message:



so maybe you left out a "use"?


chrism01 06-27-2012 08:34 PM

Start by amending
Code:

#! /usr/bin/perl -w


#use strict;
#use DBI;
#use Mysql::Statement;
#use strict;
#use warnings;
use DBI ;
#my @ary = DBI->available_drivers();
#print join("\n", @ary), "\n";

#use DBI;
#use Mysql;

to
Code:

#! /usr/bin/perl -w

use locale;            # Ensure correct charset for eg 'uc()'
use DBI;                # Database connections
use strict;            # Enforce declarations

These var names
Code:

$database = "mydb"; # database to connect to
$user = "root";
$host = "127.0.0.1"; # host
$pw = "XXXXXX";

do not match these
Code:

$dsn = "DBI:mysql:database=$database;host=$hostname;port=$port" ;
I strongly advise dropping the cgi bit and just get the core code working as a cmd line .pl, then you can extend it to work as a cgi.

http://perldoc.perl.org/
http://www.perlmonks.org/?node_id=284436
http://www.perlmonks.org/?node=Tutorials

You can run a pre-flight check thus
Code:

perl -wc myprog.pl
Ensure you ALWAYS leave -w switch on (equiv to 'use warnings) and also the 'use strict' cmd; they'll save you a boat-load of trouble later.

xman fiji 06-27-2012 09:13 PM

now i m getting this...

Execution of ./scriptfTesting.cgi aborted due to compilation errors.
root@Ubuntu-Server:/var/www/corporate/cgi-bin# ./scriptfTesting.cgi
Global symbol "$database" requires explicit package name at ./scriptfTesting.cgi line 41.
Global symbol "$user" requires explicit package name at ./scriptfTesting.cgi line 42.
Global symbol "$host" requires explicit package name at ./scriptfTesting.cgi line 43.
Global symbol "$pw" requires explicit package name at ./scriptfTesting.cgi line 44.
Global symbol "$dsn" requires explicit package name at ./scriptfTesting.cgi line 53.
Global symbol "$database" requires explicit package name at ./scriptfTesting.cgi line 53.
Global symbol "$hostname" requires explicit package name at ./scriptfTesting.cgi line 53.
Global symbol "$port" requires explicit package name at ./scriptfTesting.cgi line 53.
Global symbol "$dbh" requires explicit package name at ./scriptfTesting.cgi line 57.
Global symbol "$dsn" requires explicit package name at ./scriptfTesting.cgi line 57.
Global symbol "$user" requires explicit package name at ./scriptfTesting.cgi line 57.
Global symbol "$password" requires explicit package name at ./scriptfTesting.cgi line 57.
Global symbol "$myquery" requires explicit package name at ./scriptfTesting.cgi line 59.
Global symbol "$execute" requires explicit package name at ./scriptfTesting.cgi line 61.
Global symbol "$connect" requires explicit package name at ./scriptfTesting.cgi line 61.
Global symbol "$myquery" requires explicit package name at ./scriptfTesting.cgi line 61.
Global symbol "@data" requires explicit package name at ./scriptfTesting.cgi line 75.
Global symbol "$execute" requires explicit package name at ./scriptfTesting.cgi line 75.
Global symbol "@data" requires explicit package name at ./scriptfTesting.cgi line 77.
Global symbol "@data" requires explicit package name at ./scriptfTesting.cgi line 78.
Global symbol "@data" requires explicit package name at ./scriptfTesting.cgi line 79.
Global symbol "@data" requires explicit package name at ./scriptfTesting.cgi line 80.
Global symbol "@data" requires explicit package name at ./scriptfTesting.cgi line 81.
Execution of ./scriptfTesting.cgi aborted due to compilation errors.
root@Ubuntu-Server:/var/www/corporate/cgi-bin#

what should i do now

chrism01 06-27-2012 09:18 PM

As per my comment
Code:

use strict;            # Enforce declarations
means what it says; you have to declare your vars.
See the links I gave you ..
Here's a short extract from one of my progs

Code:

sub program_ctrl
{
    my (
        %assigned_stages,      # list of stages set in cfg file
        $curr_stage,            # stage to process
        $hastus_file,          # Hastus file to get
        %bus_stops              # x-ref bnum:pos => (sign) id
        );
.
.
.
}

Do use subs (aka functions) to break your code into manageable pieces.

You should have started with that pre-flight check cmd and fixed any errors BEFORE you try to actually run it.

xman fiji 06-27-2012 09:51 PM

but how can i declare the vars and where should i do that ...?

chrism01 06-27-2012 11:53 PM

If you read those links you'll see I've just shown you how ...

xman fiji 07-10-2012 07:51 PM

any other way ....still cant get those links work ....

chrism01 07-11-2012 08:05 PM

1. Drop the space in the shebang line
Code:

#! /usr/bin/perl -w

# should read

#!/usr/bin/perl -w

Quote:

any other way ....still cant get those links work ....
What does that mean; please use proper English and describe specific problems; not 'it doesn't work' ..


All times are GMT -5. The time now is 10:46 PM.