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


Quote Reply
Re: for loop - MY LOGIC IS FLAWED In reply to
in the line
for ($i = 0; $i = $numrows; ++$i)
you are setting $i to the number of rows instead of doing a boolean comparison like <= or == but in perl you can do:
for (0 .. $numrows)

at any rate, you are doing more work than you need. you can just do:

Code:
while (my ($url) = $sth->fetchrow_array) {
# do work
}
$sth->finish;
-- Gordon


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';