Can someone tell me what is wrong with my for looping? This script goes through a mysql database and checks the validity of each url. After all the urls have been checked it just keeps looping and DBI generates an error that says "fetchrow_array failed: fetch() without execute()" over and over. I think the problem has to do with my misunderstanding of the line $numrows = $sth->rows. Please help me, here is my code:
#!/usr/bin/perl
use LWP::UserAgent;
use DBI;
$db_database = "db";
$db_uid = "root";
$db_pwd = "password";
($ua = LWP::UserAgent->new)->timeout(20); #actually set timeout
$dbh = DBI->connect ("DBI:mysql:$db_database".$mysqlsock, $db_uid, $db_pwd) or die("could not connect to db\n");
$sth = $dbh->prepare("SELECT url FROM files");
$sth -> execute();
$numrows = $sth->rows;
print "\n\n";
for ($i = 0; $i = $numrows; ++$i) {
$url = ($sth->fetchrow_array);
if(($ua->request(HTTP::Request->new('HEAD', $url)))->is_success()) {
$validity = "link works";
} else {
$validity = "link sucks";
$valid_update = $dbh->do("UPDATE files SET valid = valid + 1 WHERE url = '$url'");
}
print "$validity\n$url\n\n";
}
#!/usr/bin/perl
use LWP::UserAgent;
use DBI;
$db_database = "db";
$db_uid = "root";
$db_pwd = "password";
($ua = LWP::UserAgent->new)->timeout(20); #actually set timeout
$dbh = DBI->connect ("DBI:mysql:$db_database".$mysqlsock, $db_uid, $db_pwd) or die("could not connect to db\n");
$sth = $dbh->prepare("SELECT url FROM files");
$sth -> execute();
$numrows = $sth->rows;
print "\n\n";
for ($i = 0; $i = $numrows; ++$i) {
$url = ($sth->fetchrow_array);
if(($ua->request(HTTP::Request->new('HEAD', $url)))->is_success()) {
$validity = "link works";
} else {
$validity = "link sucks";
$valid_update = $dbh->do("UPDATE files SET valid = valid + 1 WHERE url = '$url'");
}
print "$validity\n$url\n\n";
}