I am running a MySQL query and trying to output the data into column format. The data itself comes from a MySQL database, and then I have an array of data labels that I hard-coded (@array) that the database data needs to match up with.

my $counter = 0; while (@ary = $sth->fetchrow_array ()) { if ($ary[$counter] == NULL){ print join("\t",@array[$counter]), " ","\n"; $counter++; } else { print join("\t",@array[$counter]), $ary[$counter],"\n"; } $counter++; }

The problem seems to be multi-fold. First, my MySQL query is returning 10 rows when I need just one row (I realize this is not a question for the Perl monks, but I promise the next one is).

The main issue is that the while iteration is only running 10 times (the number of rows that fetchrow_array is retrieving) and there are more than 10 possible data labels that are to be printed in the left column of the data output. Some of the data will not yet be in the database (and it is not at the end of the data query but rather in some of the middle fields), so I tried to skip the null data fields by incrementing count, but then the problem still remains that the while loop is not going to retrieve the last several data fields to output because its already iterated 10 times and there are still several more data fields in array beyond that. I need to know how to print all labels in my hardcoded @array and also retrieve all possible data fields from the MSQL query so that for each possible data field there is a label printed to the left, regardless of whether there is data to the right of it.

Thanks in advance for any help.

In reply to formatting output from a mysql query in column format by thuperuser

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.