Hey

Sorry, a newbie type of question, but somehow I never gotten this to work when I tried. Basically I want to retrieve all values from a MySQL database and print it out in a table. The below coding works and displays all the values I want it to display. However, It uses fetchrow_hash, which retrieves the first row values of the table and then calls a while statement using a fetchrow_array, which retrieves all the values except the first row. How can I make this code so it gives it a bit more efficiency by not having to call both fetchrow_hash and array? Isn't there a way just to do something like a while (%hash = $sth->fetchrow_array) to retrieve all the values at once? I tried this but the my script died on me and seemed to slow my server down dramatically? ahh~ I just have a feeling that this is such a simple newbie mistake that I'm doing wrong. It seems like the fetchrow-hash is retrieves ALL the mysql db results (including column names that are not listed in my SELECT statement?) and therefore theirs room for improving efficiency.

print <<EOF; Company Info:<br> <table cellpadding="2" cellspacing="0" border="0" width="100%"> <tr> <td>ID</td> <td>Name</td> <td>Company</td> <td>Country</td> <td>File</td> <td>Summary</td> <td>Date Submitted</td> </tr> EOF #retrieve business plans $ci_blah = "mypassword"; use DBI; $dbh_m = DBI->connect('DBI:mysql:companies','myusername',$ci_bla +h) or die "Couldn't connect to database: " . DBI->errstr; $sql_m = "SELECT id,firstname,lastname,country,file_name,summ +ary,date FROM companies_db"; $sth_m = $dbh_m->prepare($sql_m) or die "preparing: ",$dbh_m- +>errstr; $sth_m->execute or die "executing: ", $dbh_m->errstr; $results = $sth_m->fetchrow_hashref; print " <tr>"; print " <td>$results->{'id'}</td>"; print " <td>$results->{'firstname'} $results{'lastname'}</td>"; print " <td>$results->{'company'}</td>"; print " <td>$results->{'country'}</td>"; print " <td>$results->{'file_name'}</td>"; print " <td>$results->{'summary'}</td>"; print " <td>$results->{'date'}</td>"; print " </tr>"; while (@data = $sth_m->fetchrow_array()) # keep fetching until # there's nothing left { print " <tr>"; print " <td>$data[0]</td>"; print " <td>$data[1] $data[2]</td>"; print " <td>$data[3]</td>"; print " <td>$data[4]</td>"; print " <td>$data[5]</td>"; print " <td>$data[6]</td>"; print " <td>$data[7]</td>"; print " </tr>"; } $sth_m->finish; $dbh_m->disconnect; print "</table>";


Anthony

In reply to Fetchrow_hash/array question with DBI/Perl by perleager

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.