$sth->rows() is what you want, but you should be advised that $sth->rows() is not guaranteed to give you an accurate count until you fetch all of the rows in the result set. With MySQL you should be fine calling $sth->rows() before you fetch all of the rows from the database unless you set the mysql_use_result attribute on the sth, but then again you would have the same problem with php if you used mysql_use_result().

So you can do something like this:
$sth = $dbh->prepare($sql); while (my @results = $sth->fetchrow_array) { do_something(@results) } my $count = $sth->rows();

You probably will also want to 'use strict' and 'use warnings' in your code. They will make your code easier to debug because they keep you from shooting yourself in foot in some of the most common ways. One other thing you might want to look at is Template-Toolkit or HTML::Template both of wich will allow you to remove the html from you code, so that your program will be easier to read.


In reply to Re: DBI, From PHP to Perl by tantarbobus
in thread DBI, From PHP to Perl by dstefani

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.