Just a hint: it looks as though you're working without warnings or strict. While their restrictions cause extra work, they will catch many typos and will prevent you from leaking variables across scopes you thought were independent - both of which can cause very subtle bugs that can be almost impossible to track down. Do yourself a favour, get in the habit of using my generously and make it second nature to start every script with #!/usr/bin/perl -w
use strict;
(Or, if you're using a reasonably recent version of Perl, use warnings; rather than the -w.)
In your case f.ex, the following code:
my $sth = $dbh->prepare ("SELECT id, body FROM news");
$sth->execute ();
while (my ($news_id, $news_body) = $sth->fetchrow_array()) {
my $sth = $dbh->prepare ("SELECT COUNT(id) FROM comments WHERE id
+= $news_id");
$sth->execute (); # was this missing from your snippet accidentall
+y?
my ($comments) = $sth->fetchrow_array();
print "$news_id, $news_body, $comments";
}
while rather not recommendable as a casual reader might confuse the inner and outer $sth with one another, would at least have worked.
Makeshifts last the longest. |