You can join tables in different databases if they are on the same server. Also consider using placeholders (?) in the queries.
For example
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect('dbi:mysql:alicenet','user','password',
{mysql_enable_utf8 => 1,RaiseError => 1,PrintError => 1})
or die "Connection Error: $DBI::errstr\n";
my $where = 'WHERE M.failed < 3
AND M.lasttry < (NOW() - INTERVAL 90 MINUTE)';
my $sql = "SELECT COUNT(id)
FROM mailcue AS M
$where";
#count the messages
my ($count) = $dbh->selectrow_array($sql);
#max number of cued messages to fetch
my $max = ($count < 180)? 30 : int($count/6);
$sql = "SELECT U.email,M.subject,M.message,M.id,M.failed
FROM mailcue as M
LEFT JOIN anetauth.userauth as U
ON U.id = M.userid
$where
ORDER BY M.id
LIMIT ?";
my $sql_fail = "UPDATE mailcue
SET lasttry = NOW(),failed = ?
WHERE id = ?";
my $sth_fail = $dbh->prepare($sql_fail);
my $sql_ok = 'DELETE FROM mailcue
WHERE id = ?';
my $sth_ok = $dbh->prepare($sql_ok);
my $ar = $dbh->selectall_arrayref($sql,undef,$max);
for (@$ar){
my ($email_addr,$subject,$message,$id,$failed) = @$_;
# replace print with email create and send
print "
--------------------
to = $email_addr
subject = $subject
body = $message
id = $id
failed = $failed
==================";
my $response = 'fail'; # test
if ($response eq ''){ # change to suit
$sth_ok->execute($id);
} else {
$sth_fail->execute($failed+1,$id);
}
}
poj |