punch_card_don has asked for the wisdom of the Perl Monks concerning the following question:
What possible reasons could cause my script to "hang" after outputting just 50 of 140 rows?
Here's a simple script snippet. We've selected all 140 rows from a mysql table, read them into a list of lists, and then cycle through the data structure to output to browser. Builds the data structure just fine, but then hangs up at about the 50th line of output.
BUT - if make one tiny change, outputting the inner output loop index $b instead of the array value $data$a$b, it runs to completion just fine.
??????????????????????
This script hangs.$| = 1; print "Content-type: text/html\n\n"; # get records @results = (); $sql = "SELECT * FROM my_table"; print "<br>sql = $sql<p>\n"; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->err +str); $sth->execute() or die("Could not execute!" . $dbh->errstr); print "<br>executed sql<p>\n"; $k = 0; while (@results = $sth->fetchrow_array()) { $data[$k] = [ @results ]; $k++; } $sth->finish; for $a (0 .. $#data) { print "<br>$a => "; for $b (0 .. $#{$data[$a]}) { print ", $data[$a][$b]"; } } print "<br>Done\n";
But change $data[$a][$b] in the output loop to just $b, and it runs just fine.
Update: It appears to have nothing to do with which 50 lines it outputs. If I change teh outer output loop starting index to, for example, 30, then it outputs just fine to ~ row 80, then, hangs up indefinitely.
Update 2: It appears to have nothing to do with the volume of data output. If I change the central output code to something like
print ",asd5f4as6d5f4as6d5f4a6s5d4f6sa5d4f6as5d4f6as5d4f, $b";
which creates several times more data to output than the original
print ", $data[$a][$b]";
yet it runs to completion with no trouble.
Update 3 - it appears to have something to do with the way the data structure is compiled. If I build the list of lists like this;
It outputs all 138 rows and all values just fine. Hmmmm....for $i (0 .. 137) { print "<br>$i => "; for $j (0 .. 18) { $data[$i][$j] = "D".$i."B".$j; } }
Significant Update - OK, it appears to have something important to do with nul fields. Not all columns in the db table have values in them. Some are empty, nul. I added a line to fill all elements of @results with some text:
and got told by Perl: "Use of uninitialized value in string eq ". Aha.if ($results[$j] eq "") {....}
So, when a table has x-columns, but some of them are empty, nul,
does NOT initialize all the x-elements of @results???while (@results = $sth->fetchrow_array()) {.....}
If this is the case, then it must be getting "hung" on all the error messages as I try to output hundreds of uninitialized array elements.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simple db select output script hanging up.
by shmem (Chancellor) on Jun 22, 2007 at 17:15 UTC | |
by punch_card_don (Curate) on Jun 22, 2007 at 17:22 UTC | |
by shmem (Chancellor) on Jun 22, 2007 at 17:49 UTC | |
|
Re: Simple db select output script hanging up.
by ozone (Friar) on Jun 22, 2007 at 17:49 UTC | |
by punch_card_don (Curate) on Jun 22, 2007 at 18:09 UTC | |
|
Re: Simple db select output script hanging up.
by jeanluca (Deacon) on Jun 22, 2007 at 16:55 UTC | |
by punch_card_don (Curate) on Jun 22, 2007 at 17:06 UTC | |
|
Re: Simple db select output script hanging up.
by graff (Chancellor) on Jun 23, 2007 at 15:19 UTC | |
by jZed (Prior) on Jun 23, 2007 at 17:33 UTC |