Welcome to WebmasterWorld Guest from 54.158.36.59

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

can't locate object method connect error

     
7:19 pm on Aug 14, 2009 (gmt 0)

10+ Year Member



Any ideas what causes this error?
[root@localhost bin]# /etc/myscripts/add_squid_users.pl
Can't locate object method "connect" via package "Mysql" (perhaps you forgot to load "Mysql"?) at /etc/myscripts/add_squid_users.pl line 15.


#!/usr/bin/perl

# PERL MODULE
use Mysql;

# CONFIG VARIABLES
$host = "remote-server";
$database = "db";
$tablename = "pastblswd";
$user = "user";
$pw = "pw";

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

# DEFINE A MySQL QUERY
$myquery = "SELECT user, password, enabled, customers_id FROM passwd WHERE UpdateOnNextCycle=1";

# EXECUTE THE QUERY FUNCTION
$execute = $connect->query($myquery);

while (@results = $execute->fetchrow()) {

#if enabled =1 then add the user#!/usr/bin/perl

# PERL MODULE
#use mysql;
use DBD::mysql;

# CONFIG VARIABLES
$host = "remote-mysql4.example.com";
$database = "squid";
$tablename = "passwd";
$user = "squid";
$pw = "5YpFnrfE5aV6Sq5Mm3OB";

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

# DEFINE A MySQL QUERY
$myquery = "SELECT user, password, enabled, customers_id FROM passwd WHERE UpdateOnNextCycle=1";

# EXECUTE THE QUERY FUNCTION
$execute = $connect->query($myquery);

while (@results = $execute->fetchrow()) {

#if enabled =1 then add the user
if ($results[2] == 1 )
{
#htpasswd command add
system("htpasswd -b /etc/squid/squid_passwd ".$results[0]." ".$results[1]);

}

#if enabled =0 then delete the user
if ($results[2] == 0 )
{
#htpasswd command del
system("htpasswd -D ".$results[0]);
}

#after which we update the UpdateOnNextCycle flag to 0 and set UsedDemo to 1
$execute_upd = $connect->query("
UPDATE passwd
SET UpdateOnNextCycle = 0,
UsedDemo = 1
WHERE customers_id = ".$results[3]."
");

}

{
#htpasswd command add
system("htpasswd -b /etc/squid/squid_passwd ".$results[0]." ".$results[1]);

}

#if enabled =0 then delete the user
if ($results[2] == 0 )
{
#htpasswd command del
system("htpasswd -D ".$results[0]);
}

#after which we update the UpdateOnNextCycle flag to 0 and set UsedDemo to 1
$execute_upd = $connect->query("
UPDATE passwd
SET UpdateOnNextCycle = 0,
UsedDemo = 1
WHERE customers_id = ".$results[3]."
");

}

[edited by: phranque at 9:16 pm (utc) on Aug. 16, 2009]
[edit reason] exemplified domain [/edit]

7:45 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I'm unfamiliar with this module, I use DBI - but from the samples I see, maybe this

$connect = Mysql->connect($host, $database, $user, $pw);

needs to be this

$connect = Mysql->Connect($host, $database, $user, $pw);

Case sensitive? And of course, you are sure the module is loaded? (Probably, otherwise you'd get "cannot locate package "Mysql" in @INC)

7:53 pm on Aug 14, 2009 (gmt 0)

10+ Year Member



what module do you use? ie how is DBI different?
is the new mysql module better, that's what I had commented out above, where do I download that from?

[edited by: jackvull at 7:58 pm (utc) on Aug. 14, 2009]

7:57 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



or perhaps the module isn't loaded in one of the paths specified in The @INC Array [perl.com]?
once you figure out where the Mysql (if that's what the module is actually named) module is loaded you can add directories to @INC [perldoc.perl.org] with the "use lib" syntax if necessary.
8:00 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



there are several references to DBI.pm and usage of the DBI module in the Perl Server Side CGI Scripting forum Charter [webmasterworld.com].
8:00 pm on Aug 14, 2009 (gmt 0)

10+ Year Member



where are the modules specified?
this page says nothing about using DBI in front of the MySQL part: [snip]

[edited by: phranque at 8:12 pm (utc) on Aug. 14, 2009]
[edit reason] charter [webmasterworld.com] [/edit]

8:05 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



where are the modules specified?

you specify your module in the "use" statement.
the module name is specified by the name of the .pm file and possibly the name of the library subdirectory in which it resides.
not sure if i understand your question precisely.

8:10 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



about using DBI in front of the MySQL part

DBD::mysql - MySQL driver for the Perl5 Database Interface (DBI) - search.cpan.org [search.cpan.org]
8:11 pm on Aug 14, 2009 (gmt 0)

10+ Year Member



sorry :0

you said:
or perhaps the module isn't loaded in one of the paths specified in The @INC Array?
once you figure out where the Mysql (if that's what the module is actually named) module is loaded you can add directories to @INC with the "use lib" syntax if necessary.

so if I just state use MySQL at the top of the perl script, why do I get all of these errors:
Can't locate mysql.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at ./add_squid_users.pl line 4.
BEGIN failed--compilation aborted at ./add_squid_users.pl line 4.
[root@localhost myscripts]#

8:24 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



so if I just state use MySQL at the top of the perl script, why do I get all of these errors

that is only one error message.
your module either doesn't have that name or wasn't installed in one of the paths listed in that error message.
please consult the several links provided above for answers.
there are several examples of code that should work.

8:31 pm on Aug 14, 2009 (gmt 0)

10+ Year Member



do I need DBI then?
I'm not sure how to check what's installed and not 100% on DBI vs DBD.

In the example I loooked up it just said use MySQL and the rest of the code should work as coded. What's that module?

[root@localhost myscripts]# perl -e 'use DBI; print $DBI::VERSION,"\n";'
1.609
[root@localhost myscripts]#

Which module do the
Mysql->connect commands come with? Doesn't look like DBI

9:38 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



this works for me:
$dataSourceName = "DBI:mysql:database=dbname;host=localhost";

use DBI;
$drh = DBI->install_driver('mysql');
$dbh = $drh->connect($dataSourceName, 'user', 'password', {});

4:42 pm on Aug 16, 2009 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Just curious phranque, that works even with the use command after DBI:...?

Long story short, try using the DBI method, it's almost always already installed on most servers. The advantage is it connects with many databases, not just mySQL.

what module do you use?

I'm unfamiliar with this module, I use DBI

Did you try this? Presuming you did and it's still broken . . .

$connect = Mysql->Connect($host, $database, $user, $pw);

if I just state use MySQL at the top of the perl script, why do I get all of these errors:
Can't locate mysql.pm in @INC (@INC contains: /usr/....

This is very different than the original post, in which you indicated a different error. This says the module is not installed, as everyone's telling you.

Try the DBI methods . . .

#!/usr/bin/perl
print "content-type:text/html\n\n";
$sql_login = 'user-login-name';
$sql_pass = 'user-pass';
$db_name = 'database-name';
$db_host = 'localhost'; # or remote mysql server name
# if left blank, this defaults to localhost

use DBI;
$conn_string = "DBI:mysql:$db_name";
if ($db_host) { $conn_string .= ":$db_host"; }
$dbh = DBI->connect("$conn_string",$sql_login,$sql_pass);

## Now do a test select; modern versions of mySQL combine
## prepare and execute into the do statement

$sth = $dbh->prepare("select fieldname from table;");
$rv = $sth->execute or die($dbh->errstr);
while(($field)=$sth->fetchrow_array) {
print "$field <br>\n";
}
$sth->finish;

9:08 pm on Aug 16, 2009 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Just curious phranque, that works even with the use command after DBI:...?

rocknbil, assuming you are referring to this?

$dataSourceName = "DBI:mysql:database=dbname;host=localhost";

before/after doesn't matter there.
that is simply a variable (string) assignment which doesn't require an external module.
i'm sure you knew that...
=8)

9:14 pm on Aug 16, 2009 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Any ideas what causes this error?
[root@localhost bin]# /etc/myscripts/add_squid_users.pl
Can't locate object method "connect" via package "Mysql" (perhaps you forgot to load "Mysql"?) at /etc/myscripts/add_squid_users.pl line 15.

#!/usr/bin/perl

# PERL MODULE
use Mysql;

so if I just state use MySQL at the top of the perl script, why do I get all of these errors:
Can't locate mysql.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi
...
/usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at ./add_squid_users.pl line 4.

the module name is case sensitive so make sure you "use" the right module.

4:31 pm on Aug 17, 2009 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



No, I meant setting the use command after referencing it,

$dataSourceName = "DBI:mysql:database=dbname;host=localhost";

use DBI;

instead of

use DBI;
$dataSourceName = "DBI:mysql:database=dbname;host=localhost";

'Spose it doesn't matter, just style.

7:36 am on Aug 18, 2009 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



i guess you could call it style, but strictly speaking the assignment statement doesn't use anything in the DBI module.
the DBI->connect method does.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month