Hi everyone!

Currently I am learning how to wed MySQL with Perl, and use it for database/internet use. To do this, I of course am using the DBI module. I understand how to search for an id field in Perl by using DBI, but I don't know how I can display a whole table from MySQL in a Perl script. Is there a way to do this?

Currently, my MySQL table has columns:
+----+----------+----------+-------+-----------------+ | id | nickname | password | socks | favorite_number | +----+----------+----------+-------+-----------------+ | 1 | Cowlick | salty | 0 | 4 | | 2 | andy | bandy | 3 | 7 | +----+----------+----------+-------+-----------------+

Which is simply an example for a short tutorial site. Also the Perl script I am using is this:
#!/usr/bin/perl -w use warnings; use strict; use DBI; my $dsn = 'DBI:mysql:test:localhost'; my $db_user_name = ''; my $db_password = ''; my ($id, $password); my $dbh = DBI->connect($dsn, $db_user_name, $db_password); my $input_nickname = 'Cowlick'; my $input_password = 'salty'; my $sth = $dbh->prepare( qq/select id, password from users where nickname = '$input_nickname'/); $sth->execute(); ($id, $password) = $sth->fetchrow_array(); $sth->finish(); if ($input_password eq $password) { print "\nLogin Successful!\n"; print "Here are the stats:\n"; print "="x20,"\n"; print "id = $id, nick=$input_nickname, pass = $password\n"; } $dbh->disconnect();

So far this program does what I want it to, but when I try adding the line:
my $sth = $dbh->prepare( qq/select * from users/);

instead of the line:
my $sth = $dbh->prepare( qq/select id, password from users where nickn +ame = '$input_nickname'/);

Basically my question is what the title says: Is there a way I can display all of the contents of that MySQL table in the Perl script? Any help on this subject would be much appreciated. I have found that learning how to use Perl with MySQL is actually fun, and somewhat easy. I just don't know how to this type of thing. Maybe I am not in the right frame of mind ... anyways, any help would be appreciated!

Andy Summers

In reply to 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.