user2772936
user2772936

Reputation: 55

Executing two queries in one perl script

i'm a newbie on perl scripting and i found a problem while trying to execute two sqls, here you have the code, for sure not the best one.

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!";

}

So, if i comment this part of the code

# 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);

Everything works fine, but when i try to execute first this one, script stops responding exactly on this line:

while($sth->fetch) {

I tried to debug the code, look for tutorials, read a lot of pages and cannot figure where is the problem :(

Regards.

**** UPDATE ********

I think i found the problem after some more debug but not the solution. On the second sql named $query i passed the variable $lastid that i get on the first sql, see:

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

If i change the $lastid for, as an example, 13330506, everything works, so seems to be that there is an issue about how this variable is passed. The strange thing is that when i print the $query with $lastid inside the content of $lastid is correct, the number appears... strange, at least for me.

Upvotes: 2

Views: 1585

Answers (1)

user1498339
user1498339

Reputation: 629

If you read the documentation http://search.cpan.org/dist/DBI/DBI.pm you'll see there is no ->fetch function, but there are various fetch methods:

@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 );

Each one returns a reference you should store in variable for later use, for example:

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

Then, you can use @row or $data inside the loop to retrieve the data you need.

Upvotes: 1

Related Questions