Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found