2013-01-03 62 views
1

我開始爲一些cron作業編寫一些Perl腳本,這些腳本將查詢數據庫併發出有關即將發生的事件的提醒。我對Perl中的數據庫訪問頗爲陌生,因爲迄今爲止,我的大部分工作都是使用PHP在Web端進行的。無論如何,第一個查詢工作正常,生成一個臨時輸出文件,然後我回讀該輸出文件,以循環查詢結果,查找第一個查詢中發現的用戶的特定事件。Perl DBD錯誤功能dbName.GLOB不存在

,我運行到現在,得到以下錯誤的問題:

./remind.pl 
DBD::mysql::st execute failed: FUNCTION dbName.GLOB does not exist at ./remind.pl line 41. 
SQL Error: FUNCTION dbName.GLOB does not exist 

這是我的Perl代碼事先的任何援助

$host = 'localhost'; 
$database = 'dbName'; 
$user = 'user'; 
$password = 'password'; 

use POSIX qw(strftime); 
use List::MoreUtils qw(uniq); 
use Mail::Sendmail; 
use DBI; 

$dt = strftime("%Y%m%d%H%M%S", localtime(time)); 
$List30 = "../tmp/queries/30DayUserList.$dt"; 
open my $UserList30Day, ">> $List30" or die "Can't create tmp file: $!"; 

$dbh = DBI->connect('dbi:mysql:dbName',$user,$password) or die "Connection error: $DBI::errstr\n"; 

$sql = "SELECT DISTINCT user FROM shows WHERE initial_date BETWEEN CURDATE() AND CURDATE() + INTERVAL 30 DAY"; 
$sth = $dbh->prepare($sql); 
$sth->execute or die "SQL Error: $DBI::errstr\n"; 

while (@jeweler = $sth->fetchrow_array()) { 
print $UserList30Day "$user[0]\n"; 
} 
close $UserList30Day; 

open my $UserIDList, "< $List30" or die "Can't open temp file: $List30"; 

while ($id = $UserIDList) { # Read in User ID from temp file as $id 
# Query for show information for next 30 days 

my $sql = "SELECT shows.initial_date, shows.initial_time, shows.hostess_key, hostess.hostess_fname, hostess.hostess_lname, hostess.primary_phone, hostess.address1, hostess.address2, hostess.city, hostess.zipcode, hostess.state 
     FROM shows, hostess 
     WHERE shows.user = $id 
     AND initial_date BETWEEN CURDATE() AND CURDATE() + INTERVAL 30 DAY 
     AND shows.hostess_key = hostess.hostess_key"; 
my $sth = $dbh->prepare($sql); 
$sth->execute or die "SQL Error: $DBI::errstr\n"; 

# Iterate thru query results to create output data 
while (@row = $sth->fetchrow_array()) { 
    $content = "Reminder: You have a show for $row[3] $row[4] coming up on $row[0] at $row[1].\n"; 
    $content .= "Location: $row[6] \n"; 
    if ($row[7] != '') { 
     $content .= "   " . $row[7] . "\n"; 
    } 
    $content .= "   $row[8], $row[10] $row[9] \n"; 
    $content .= "Phone: $row[5] \n"; 
} 

%mail = (To => 'email', 
    From => 'email', 
    Subject => 'Just another test', 
    Message => $content 
    ); 

# sendmail(%mail) or die $Mail::Sendmail::error; 
print %mail; 
} 
close $UserList30Day; 

感謝。

+4

'的$ id = $ UserIDList'不 「讀從臨時文件的用戶ID」。它只是將'$ UserIDList'(一個文件句柄)賦給'$ id'。另外,你錯過了'使用警告;嚴格使用;'頂部,並且你有SQL注入問題。使用參數化查詢! – melpomene

回答

3
while ($id = $UserIDList) { 

應該

while ($id = <$UserIDList>) { 
    chomp; 
+0

,別忘了'chomp' ... – pavel

+0

@pavel,補充,謝謝 – ikegami