Hello, I am currently migrating from PHP to Perl/CGI.
In PHP there is a nice little fuction called mysql_num_rows() that returns the number of rows from a SELECT query.

Using DBI however, I can't see a way to get my total returned rows count without doing a seperate COUNT(*) query beforehand and then repeating the same query in a non-grouping fashion. For instance... a chunk of code from a script I'm working on:

## chunk 'O' code $sql = "SELECT COUNT(*) "; $sql .= "FROM sc_products "; $sql .= "WHERE name LIKE '%$search_name%' "; $sql .= "AND keywords LIKE '%Color%'"; $sth = $dbh->prepare($sql); $sth->execute || $error->LogError("Error opening database connection f +or product count in search.cgi".$dbh->errstr, 0); # get my count my $count = $sth->fetchrow_array; $sth->finish; $sql = "SELECT name, image2 "; $sql .= "FROM sc_products "; $sql .= "WHERE name LIKE '%$search_name%' "; $sql .= "AND keywords LIKE '%Color%'"; $sth = $dbh->prepare($sql); $sth->execute || $error->LogError("Error opening database connection f +or product count in search.cgi".$dbh->errstr, 0); while(my @results = $sth->fetchrow_array) { ## Use my count in here for some cool table formatting... print "<tr><td class=\"imgs\">... } $sth->finish; $dbh->disconnect();
I look forward to your input.
Thank you very much,
dstefani

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