Welcome to WebmasterWorld Guest from 54.198.134.104
#!/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]
$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)
[edited by: phranque at 8:12 pm (utc) on Aug. 14, 2009]
[edit reason] charter [webmasterworld.com] [/edit]
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.
about using DBI in front of the MySQL part
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]#
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.
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
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;
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)
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.
$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.