homepage Welcome to WebmasterWorld Guest from 54.204.127.56
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
can't locate object method connect error
jackvull




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

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]

 

rocknbil




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

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)

jackvull




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

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]

phranque




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

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.

phranque




msg:3972204
 8:00 pm on Aug 14, 2009 (gmt 0)

there are several references to DBI.pm and usage of the DBI module in the Perl Server Side CGI Scripting forum Charter [webmasterworld.com].

jackvull




msg:3972205
 8:00 pm on Aug 14, 2009 (gmt 0)

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]

phranque




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

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.

phranque




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

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]

jackvull




msg:3972220
 8:11 pm on Aug 14, 2009 (gmt 0)

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]#

phranque




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

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.

jackvull




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

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

phranque




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

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

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


rocknbil




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

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;

phranque




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

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)

phranque




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

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.

rocknbil




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

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.

phranque




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved