Gossamer Forum
Home : General : Perl Programming :

for loop - MY LOGIC IS FLAWED

Quote Reply
for loop - MY LOGIC IS FLAWED
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";
}


Subject Author Views Date
Thread for loop - MY LOGIC IS FLAWED allthingsfree 2181 Sep 24, 2000, 4:02 PM
Post Re: for loop - MY LOGIC IS FLAWED
GClemmons 2094 Sep 24, 2000, 6:11 PM