Perl MySQL query Hi,
I have a thorny problem that I cannot fix, so had to do a work around. Any wise Perl programmers out there have any insight?
Standard perl DBI interface; ****CODE SAMPLE****
my $dbh = DBI->connect("DBI:mysql:$db:$db_server",$dbuser,$dbpasswd)
or die "Can't connect to the DB: $DBI::errstr\n";
my $statement = "SELECT * FROM tblgenjnl WHERE law = '$jnl_law' AND week = '$jnl_week' AND quadrant_day = '$jnl_day'";
my $sth;
$sth = $dbh->prepare($statement)
or die "Couldn't prepare the query: $sth->errstr\n";
my $rv = $sth->execute()
or die "Couldn't execute the query: $dbh->errstr";
while(@row = $sth->fetchrow_array())
{
print "Law: $row[1]\t Quadrant: $row[2]\t Day: $row[3]\n Quote: \t $row[4]\n\n Source: $row[5]\n\n";
print "Entry: $row[6]\n Daily Guidance: $row[7] \n\n";
$quote = $row[4];
$source = $row[5];
$entry = $row[6];
$guidance = $row[7];
$text = $row[8];
}
my $rc = $sth->finish();
$rc = $dbh->disconnect;
After a few hours of digging I see that the while loop executes twice, fetching incorrect row data. I've attempted to understand why this would happen and cannot. So I wrote a work around which executes correctly.
........
while(@row = $sth->fetchrow_array())
{
print "Law: $row[1]\t Quadrant: $row[2]\t Day: $row[3]\n Quote: \t $row[4]\n\n Source: $row[5]\n\n";
print "Entry: $row[6]\n Daily Guidance: $row[7] \n\n";
$quote = $row[4];
$source = $row[5];
$entry = $row[6];
$guidance = $row[7];
$text = $row[8];
$meditation = $row[9];
last if $meditation ne "m";
}
I have a field in the db labelled "Meditation", I set the var $meditation to = "m" and within the db it never does. Therefore while loop only executes once now. This works fine thankfully.
However, I still don't know why the original code doesn't work? Any ideas?
many thanks. |