There are a few ways to display an entire table. The idea is to iterate through the rows that are returned (a list), where each row contains a numbers of elements (another list).
# prepare the SQL my $sth = $dbh->prepare('select * from table'); # execute $sth->execute(); # fetch first row my @row = $sth->fetchrow_array(); # print remaining rows while (my @row = $sth->fetch_array()) { print join(',', @row), "\n"; }
Here is a different way that uses my favorite DBI method, selectall_arrayref():
my $res = $dbh->selectall_arrayref('select * from table'); foreach my $row (@$res) { print join(',', @$row), "\n"; } # html-table style print "<table>\n"; foreach my $row (@$res) { print "\t<tr>\n"; print map { "\t\t<td>$_</td>\n" } @$row; print "\t</tr>\n"; } print "</table>\n";
If you find yourself doing a lot of the last example, check out DBIx::XHTML_Table, but HTML::Template is still the better choice. Be sure and read HTML::Template Tutorial (which i _finally_ updated to make 'Y2K+2' compliant) for info on displaying database results with it.

UPDATE:
Trimbach++ for reminding what i have so long forgotten.

Just to keep up with ease, here is how it's done in DBIx::XHTML_Table:

use DBIx::XHTML_Table; my $table = DBIx::XHTML_Table->new( 'DBI:mysql:database:host', 'user', 'pass') ) || die; $table->exec_query('select * from table'); print $table->output();
And that includes adding the names of the database columns wrapped in <th> tags. You can also stack the method calls:
print DBIx::XHTML_Table ->new('DBI:mysql:database:host', 'user', 'pass') ->exec_query('select * from table') ->output();
Of course, if that's all that DBIx::XHTML_Table did, then i would argue that a better way would be to wrap the CGI way into a subroutine ... but, it does more ... ;)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)


In reply to (jeffa) Re: How can I display an entire MySQL table in Perl? by jeffa
in thread How can I display an entire MySQL table in Perl? by bladx

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.