try this:

#!/usr/bin/perl -w use CGI; use DBI; use strict; use vars qw($dbh); BEGIN { $dbh = DBI->connect("DBI:ODBC:PM_Pages","","", { RaiseError => 1 }); } END { $dbh->disconnect if $dbh; } my $q; $q = new CGI; print $q->header, $q->start_html("District List"), $/; my $sql = "select District, DIST from Districts"; my $sth = $dbh->prepare($sql); $sth->execute(); my $cols = 3; my @rows = @{$sth->fetchall_arrayref}; # my $col_length = POSIX::ceil(@rows / $cols); my $col_length = (@rows - @rows % -$cols) / $cols; print "<table>", $/; for my $row (0 .. $col_length - 1) { print "<tr>", $/; for my $col (0 .. $cols - 1) { my $idx = $row + ($col * $col_length); last unless $idx < @rows; printf '<td><a href="%d">%s</a></td>', @{$rows[$idx]}[1,0]; print $/; } print "</tr>", $/; } print "</table>", $/; print $q->end_html, $/;

notice that you need to know the total number of rows and that you need to be able to have indexed access to the result set in order to build the table by directly iterating through the cells in column-major, row-minor order. this is why all the data is fetched into an array before display.

you specify that your table always has 600 rows, so you may be able to do this without an array, if you can get indexed (i.e. non-sequential) access to your result set. i doubt this is possible through ODBC (it probably requires a DB-specific driver and DB support for non-sequential cursors).

update: substituted working code for broken code


In reply to Re: DBI output in 3 columns by mdillon
in thread DBI output in 3 columns by Donzo

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.