Chip Monks,

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.

??????????????????????

$| = 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";
This script hangs.

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;

for $i (0 .. 137) { print "<br>$i => "; for $j (0 .. 18) { $data[$i][$j] = "D".$i."B".$j; } }
It outputs all 138 rows and all values just fine. Hmmmm....

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:

if ($results[$j] eq "") {....}
and got told by Perl: "Use of uninitialized value in string eq ". Aha.

So, when a table has x-columns, but some of them are empty, nul,

while (@results = $sth->fetchrow_array()) {.....}
does NOT initialize all the x-elements of @results???

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.




Forget that fear of gravity,
Get a little savagery in your life.

In reply to Simple db select output script hanging up. by punch_card_don

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.