in reply to formatting output from a mysql query in column format

It looks like you are missing a loop to iterate over all the COLUMNS returned from fetchrow_array.

You appear to be using the $counter - both as a row index, and as a column index.

Is there a reason you use the array slice syntax "@array[$counter]" to extract what appears to be a single item ?

"LIMIT 1" can be appended to your SQL - if you just want one row returned. There are other ways to get SQL to return the desired row - but you have not indicated what filters you require.

            "XML is like violence: if it doesn't solve your problem, use more."

  • Comment on Re: formatting output from a mysql query in column format

Replies are listed 'Best First'.
Re^2: formatting output from a mysql query in column format
by thuperuser (Initiate) on Jul 25, 2011 at 18:29 UTC

    It looks like you are missing a loop to iterate over all the COLUMNS returned from fetchrow_array.

    I probably am -- how can I implement this within the code as it is?

    Is there a reason you use the array slice syntax "@array$counter" to extract what appears to be a single item ?

    Because the labels in the @array need to match up with the fields output by MySQL...also I am completely new to perl programming, so correct me if I am making some egregious error there (I don't count running perl scripts as experience).

      If I understand what you are trying to do - you are spinning your wheels too hard.

      Let the perl modules do the work for you.

      Here is what I think you are trying to get to:

      # @array contains the field names (You call them 'labels') . # OK - so I assume they are also the field names, and we will use the +m as labels. my $counter = 0; while ( my $r = $sth->fetchrow_hashref() ) { $counter ++; print " ---- $counter ------\n"; for my $fieldname (@array){ print " $fieldname:\t" . $r->{$fieldname} . "\n"; } }
      (Untested)

                  "XML is like violence: if it doesn't solve your problem, use more."