我是一個perl腳本的新手,我在嘗試執行兩個sqls時發現一個問題,在這裏你有代碼,肯定不是最好的。在一個perl腳本中執行兩個查詢
use DBI;
use DBD::mysql;
use Socket;
use strict;
use warnings;
# CONFIG VARIABLES
my $platform = 'mysql';
my $database = 'database_name';
my $host = 'hostname';
my $port = '3306';
my $user ='user';
my $pw ='password';
# DATA SOURCE NAME
my $dsn = "dbi:mysql:$database:$host:3306";
# PERL DBI CONNECT
my $dbh = DBI->connect($dsn,$user,$pw,{RaiseError=>1,PrintError=>1}) or die "Could not connect to database: $DBI::errstr";
# READ THE LASTID OF THE DATABASE
my $queryID = "SELECT event.id from snorby.event order by event.id desc limit 1";
my $lastid = $dbh->selectrow_array($queryID);
#HIGH
while (1 == 1)
{
my $query = "SELECT event.id, inet_ntoa(iphdr.ip_src) as 'src', tcp_sport, inet_ntoa(iphdr.ip_dst) as 'dst', tcp_dport, signature.sig_name, event.timestamp, unhex(data.data_payload) from snorby.event join snorby.signature on signature.sig_id = event.signature join snorby.iphdr on event.cid=iphdr.cid and event.sid=iphdr.sid join snorby.data on event.cid=data.cid and event.sid=data.sid join snorby.tcphdr on event.cid=tcphdr.cid and event.sid=tcphdr.sid where event.id > $lastid and signature.sig_priority = '1' order by event.id";
my $sth = $dbh->prepare($query);
$sth->execute() or die "SQL Error: $DBI::errstr\n";
# BIND TABLE COLUMNS TO VARIABLES
my($eventid,$src,$sport,$dst,$dport,$signature,$timestamp,$payload);
$sth->bind_columns(undef, \$eventid, \$src, \$sport, \$dst, \$dport, \$signature, \$timestamp, \$payload);
# LOOP THROUGH RESULTS
while($sth->fetch) {
my $src_temp = inet_aton($src);
my $dst_temp = inet_aton($dst);
print "IT WORKS!";
}
所以,如果我評論的代碼
# READ THE LASTID OF THE DATABASE
my $queryID = "SELECT event.id from snorby.event order by event.id desc limit 1";
my $lastid = $dbh->selectrow_array($queryID);
一切正常的一部分,但是當我嘗試執行第一這一項,腳本將停止在這條線正好迴應:
while($sth->fetch) {
我試圖調試代碼,尋找教程,閱讀了很多頁面,並且無法確定問題出在哪裏:(
此致敬禮。
** * * UPDATE * ** * ****
我想我發現這個問題後,一些更多的調試,但不是解決辦法。在第二個SQL命名爲$查詢我通過變量$ lastid,我得到的第一個SQL,請參閱:
my $query = "SELECT stuff from table join things where event.id > **$lastid** and blablabla
如果我更改爲$ lastid,作爲一個例子,13330506,一切正常,所以似乎是否有關於如何傳遞此變量的問題。奇怪的是,當我用$ lastid在$ lastid的內容中打印$ query時是正確的,這個數字出現......很奇怪,至少對我而言。
其實,有一個'fetch'方法。這是'fetchrow_arrayref'的別名。 – cjm
更新信息,我想我找到了問題,但不是解決方案:S – user2772936
好的,所以,解決方案發現,很容易,我是白癡。我在索要一堆比數據庫上最後一個ID更大的id:facepalm:對不起浪費你的時間。 – user2772936