Mysql 在一个perl脚本中执行两个查询

Mysql 在一个perl脚本中执行两个查询,mysql,perl,Mysql,Perl,我是perl脚本的新手,在尝试执行两个SQL时发现了一个问题,这里有代码,当然不是最好的 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

我是perl脚本的新手,在尝试执行两个SQL时发现了一个问题,这里有代码,当然不是最好的

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) {
我试着调试代码,查找教程,阅读了很多页面,但无法找出问题出在哪里:(

问候

******更新*********

我想我是在进一步调试后发现问题的,但不是解决方案。在第二个名为$query的sql中,我传递了在第一个sql中获得的变量$lastid,请参见:

my $query = "SELECT stuff from table join things where event.id > **$lastid** and blablabla

例如,如果我为1333006更改$lastid,一切都正常,因此似乎存在关于此变量如何传递的问题。奇怪的是,当我在$lastid内容中打印$lastid的$query是正确的时,这个数字似乎…奇怪,至少对我来说是这样。

如果您阅读文档的话 您将看到没有->fetch函数,但有多种fetch方法:

@row_ary  = $sth->fetchrow_array;
$ary_ref  = $sth->fetchrow_arrayref;
$hash_ref = $sth->fetchrow_hashref;

$ary_ref  = $sth->fetchall_arrayref;
$ary_ref  = $sth->fetchall_arrayref( $slice, $max_rows );

$hash_ref = $sth->fetchall_hashref( $key_field );
每个变量都返回一个应存储在变量中供以后使用的引用,例如:

while ( @row = $sth->fetchrow_array ) { ... }
while (my $data = $sth->fetchrow_hashref) { ... }

然后,你可以在循环中使用@row或$data来检索你需要的数据。

实际上,有一个
fetch
方法。它是
fetchrow\u arrayref
的别名。更新信息,我想我找到了问题,但不是解决方案:SOk,所以,找到了解决方案,很简单,我是个白痴。我要的是一堆比上一个id大的id关于数据库:facepalm:抱歉浪费您的时间。